#Need help with http

1 messages · Page 1 of 1 (latest)

quasi coral
#

Can anyone give me a Zig code that adds this content data to an http request:
content: {"name":"Tom", "id":1}

sleek oyster
#

What have you tried?

glad relic
#

test case for std.http.Server will be useful

#

just go to it's definition and scroll down

quasi coral
#

I have tried nothing, and I am still looking for an example of code.

ivory monolith
quasi coral
#

I tried this code and got an error:

    try request.writeAll("{\"name\":\"Tom\",\"id\":1}");
    try request.finish();               
    try request.wait();```
```error: NotWriteable
/mnt/Archive/Downloads/zig-linux-x86_64-0.11.0/lib/std/http/Client.zig:821:22: 0x2abafc in write (hello)
            .none => return error.NotWriteable,
                     ^
/mnt/Archive/Downloads/zig-linux-x86_64-0.11.0/lib/std/http/Client.zig:828:22: 0x29dcae in writeAll (hello)
            index += try write(req, bytes[index..]);
                     ^
/mnt/Archive/Documents/Programming/Zig/hello.zig:36:5: 0x29b8ff in main (hello)
    try request.writeAll("{\"name\":\"Tom\",\"id\":1}");```
lyric quest
#

Iirc you have to request.send() before writing

#

Theres also http.Client.fetch for simpler api

#

You can use std.json to format the json from struct

quasi coral
#

There is no send function in http:

    try request.send();
        ~~~~~~~^~~~~```
lyric quest
#

What zig version are you on?

quasi coral
#

0.11.x

lyric quest
#

Ok i think the api has changed since 0.11

quasi coral
#

I got the same errors:

try request.writer().writeAll("Zig Bits!\n");
try request.finish();
try request.wait();```
```error: NotWriteable
/mnt/Archive/Downloads/zig-linux-x86_64-0.11.0/lib/std/http/Client.zig:821:22: 0x2eaedc in write (hello)
            .none => return error.NotWriteable,
                     ^
/mnt/Archive/Downloads/zig-linux-x86_64-0.11.0/lib/std/io/writer.zig:17:13: 0x2ab6b8 in write (hello)
            return writeFn(self.context, bytes);
            ^
/mnt/Archive/Downloads/zig-linux-x86_64-0.11.0/lib/std/io/writer.zig:23:26: 0x29dd40 in writeAll (hello)
                index += try self.write(bytes[index..]);
                         ^
/mnt/Archive/Documents/Programming/Zig/hello.zig:36:5: 0x29b964 in main (hello)
    try request.writer().writeAll("Zig Bits!\n");```
lyric quest
#

You may need that bit as well request.transfer_encoding = .chunked;

#

Also make sure you arent sending body with .GET

ivory sky
#

Perhaps that's a way the API could be improved in the future.

lyric quest
#

I mean the HTTP API already has changed

quasi coral
#

Can anyone let me know why I get error at the second http request?

try request.wait();
var body = request.reader().readAllAlloc(allocator, 8192) catch unreachable;
defer allocator.free(body);
std.log.info("{s}", .{body});

try request.start();
try request.wait();
body = request.reader().readAllAlloc(allocator, 8192) catch unreachable;
defer allocator.free(body);
std.log.info("{s}", .{body});```
```info: speling is difikult

error: HttpHeadersInvalid```
lyric quest
#

I don't think you can reuse request like that at least

#

yeah seems like you have to make new request everytime

#

also you should call request.finish(); and deinit the request when you are done

twin bane
# lyric quest Theres also http.Client.fetch for simpler api

on the topic of the fetch api, I made a short video explaining it https://www.youtube.com/watch?v=xSyYl186rLw

Some applications may need to fetch external data from an API or some other source. Fetch requests are a simple way to achieve this. In this video, I'll walk through how to create a simple fetch request in Zig that supports GET and POST.

Disclaimer: This code is not production safe and just for educational purposes.

You can find the code writt...

▶ Play video
twin bane
main mesa