Skip to content

Commit f2bb91c

Browse files
Added Http Post Request Example (#56)
* Added Http Post Request Example * made url variable const * polish * fix compile * fix mmap --------- Co-authored-by: jiacai2050 <dev@liujiacai.net>
1 parent 8f5850b commit f2bb91c

File tree

6 files changed

+103
-11
lines changed

6 files changed

+103
-11
lines changed
File renamed without changes.

book-src/05-02-http-post.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## POST
2+
3+
Parses the supplied URL and makes a synchronous HTTP POST request
4+
with [`request`]. Prints obtained [`Response`] status, and data received from server.
5+
6+
> Note: Since HTTP support is in early stage, it's recommended to use [libcurl](https://curl.se/libcurl/c/) for any complex task.
7+
8+
```zig
9+
{{#include ../src/05-02.zig }}
10+
```
11+
12+
[`request`]: https://ziglang.org/documentation/0.11.0/std/src/std/http/Client.zig.html#L992
13+
[`response`]: https://ziglang.org/documentation/0.11.0/std/src/std/http/Client.zig.html#L322

book-src/SUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
- [Web Programming]()
2626

27-
- [Make HTTP requests](./05-01-http-requests.md)
27+
- [HTTP Get](./05-01-http-get.md)
28+
- [HTTP Post](./05-02-http-post.md)
2829

2930
- [Algorithms]()
3031

src/01-02.zig

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,30 @@ pub fn main() !void {
2525
const md = try file.metadata();
2626
try std.testing.expectEqual(md.size(), file_size);
2727

28-
const ptr = try std.os.mmap(
29-
null,
30-
20,
31-
std.os.PROT.READ | std.os.PROT.WRITE,
32-
if (is_zig_11) std.os.MAP.PRIVATE else .{ .TYPE = .PRIVATE },
33-
file.handle,
34-
0,
35-
);
36-
defer std.os.munmap(ptr);
28+
const ptr = if (is_zig_11)
29+
try std.os.mmap(
30+
null,
31+
20,
32+
std.os.PROT.READ | std.os.PROT.WRITE,
33+
std.os.MAP.PRIVATE,
34+
file.handle,
35+
0,
36+
)
37+
else
38+
try std.posix.mmap(
39+
null,
40+
20,
41+
std.posix.PROT.READ | std.posix.PROT.WRITE,
42+
.{ .TYPE = .PRIVATE },
43+
file.handle,
44+
0,
45+
);
46+
47+
defer if (is_zig_11) {
48+
std.os.munmap(ptr);
49+
} else {
50+
defer std.posix.munmap(ptr);
51+
};
3752

3853
// Write file via mmap
3954
const body = "hello zig cookbook";

src/04-02.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub fn main() !void {
88
_ = args.skip();
99
const port_value = args.next() orelse {
1010
print("expect port as command line argument\n", .{});
11-
std.os.exit(1);
11+
return error.NoPort;
1212
};
1313
const port = try std.fmt.parseInt(u16, port_value, 10);
1414

src/05-02.zig

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const std = @import("std");
2+
const print = std.debug.print;
3+
const http = std.http;
4+
const is_zig_11 = @import("builtin").zig_version.minor == 11;
5+
6+
pub fn main() !void {
7+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
8+
defer _ = gpa.deinit();
9+
const allocator = gpa.allocator();
10+
11+
var client = http.Client{ .allocator = allocator };
12+
defer client.deinit();
13+
14+
const uri = try std.Uri.parse("http://httpbin.org/anything");
15+
16+
const payload =
17+
\\ {
18+
\\ "name": "zig-cookbook",
19+
\\ "author": "John"
20+
\\ }
21+
;
22+
23+
var req = if (is_zig_11) blk: {
24+
var headers = http.Headers{ .allocator = allocator };
25+
defer headers.deinit();
26+
27+
var req = try client.request(.POST, uri, headers, .{});
28+
errdefer req.deinit();
29+
30+
req.transfer_encoding = .{ .content_length = payload.len };
31+
32+
try req.start();
33+
var wtr = req.writer();
34+
try wtr.writeAll(payload);
35+
try req.finish();
36+
try req.wait();
37+
38+
break :blk req;
39+
} else blk: {
40+
var buf: [1024]u8 = undefined;
41+
var req = try client.open(.POST, uri, .{ .server_header_buffer = &buf });
42+
errdefer req.deinit();
43+
44+
req.transfer_encoding = .{ .content_length = payload.len };
45+
46+
try req.send(.{});
47+
var wtr = req.writer();
48+
try wtr.writeAll(payload);
49+
try req.finish();
50+
try req.wait();
51+
52+
break :blk req;
53+
};
54+
defer req.deinit();
55+
56+
try std.testing.expectEqual(req.response.status, .ok);
57+
58+
var rdr = req.reader();
59+
const body = try rdr.readAllAlloc(allocator, 1024 * 1024 * 4);
60+
defer allocator.free(body);
61+
62+
print("Body:\n{s}\n", .{body});
63+
}

0 commit comments

Comments
 (0)