#Need help with http
1 messages · Page 1 of 1 (latest)
What have you tried?
test case for std.http.Server will be useful
just go to it's definition and scroll down
I have tried nothing, and I am still looking for an example of code.
lots of http examples here https://github.com/ziglang/zig/blob/master/test/standalone/http.zig
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}");```
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
There is no send function in http:
try request.send();
~~~~~~~^~~~~```
What zig version are you on?
0.11.x
Ok i think the api has changed since 0.11
https://blog.orhun.dev/zig-bits-04/
This uses 0.11 api i guess
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");```
You may need that bit as well request.transfer_encoding = .chunked;
Also make sure you arent sending body with .GET
This might actually be a decent illustration of one way to make good errors; if this error was error.RequestNotStarted then it would naturally lead you to knowing what to try.
Perhaps that's a way the API could be improved in the future.
I mean the HTTP API already has changed
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```
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
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...
the docs show the order of operations (zig v0.12)
@quasi coral Here's how we handle writing responses in Jetzig: https://github.com/jetzig-framework/jetzig/blob/main/src/jetzig/http/Server.zig#L86
No guarantees that this is correct, but hopefully it gives you some guidance. Feel free to ask any questions !