forked from jiacai2050/zig-curl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced.zig
129 lines (110 loc) · 3.63 KB
/
advanced.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const std = @import("std");
const println = @import("util.zig").println;
const mem = std.mem;
const Allocator = mem.Allocator;
const curl = @import("curl");
const Easy = curl.Easy;
const UA = "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0";
const Response = struct {
headers: struct {
@"User-Agent": []const u8,
Authorization: []const u8,
},
json: struct {
name: []const u8,
age: usize,
},
method: []const u8,
url: []const u8,
};
fn putWithCustomHeader(allocator: Allocator, easy: Easy) !void {
const body =
\\ {"name": "John", "age": 15}
;
const headers = blk: {
var h = try easy.createHeaders();
errdefer h.deinit();
try h.add("content-type", "application/json");
try h.add("user-agent", UA);
try h.add("Authorization", "Basic YWxhZGRpbjpvcGVuc2VzYW1l");
break :blk h;
};
defer headers.deinit();
try easy.setUrl("https://httpbin.org/anything/zig-curl");
try easy.setHeaders(headers);
try easy.setMethod(.PUT);
try easy.setVerbose(true);
try easy.setPostFields(body);
var buf = curl.Buffer.init(allocator);
try easy.setWritedata(&buf);
try easy.setWritefunction(curl.bufferWriteCallback);
var resp = try easy.perform();
resp.body = buf;
defer resp.deinit();
std.debug.print("Status code: {d}\nBody: {s}\n", .{
resp.status_code,
resp.body.?.items,
});
const parsed = try std.json.parseFromSlice(Response, allocator, resp.body.?.items, .{
.ignore_unknown_fields = true,
});
defer parsed.deinit();
try std.testing.expectEqualDeep(
parsed.value,
Response{
.headers = .{
.@"User-Agent" = UA,
.Authorization = "Basic YWxhZGRpbjpvcGVuc2VzYW1l",
},
.json = .{
.name = "John",
.age = 15,
},
.method = "PUT",
.url = "https://httpbin.org/anything/zig-curl",
},
);
// Get response header `date`.
const date_header = try resp.getHeader("date");
if (date_header) |h| {
std.debug.print("date header: {s}\n", .{h.get()});
} else {
std.debug.print("date header not found\n", .{});
}
}
fn postMutliPart(easy: Easy) !void {
// Reset old options, e.g. headers.
easy.reset();
const multi_part = try easy.createMultiPart();
try multi_part.addPart("foo", .{ .data = "hello foo" });
try multi_part.addPart("bar", .{ .data = "hello bar" });
try multi_part.addPart("build.zig", .{ .file = "build.zig" });
try multi_part.addPart("readme", .{ .file = "README.org" });
defer multi_part.deinit();
try easy.setUrl("https://httpbin.org/anything/mp");
try easy.setMethod(.PUT);
try easy.setMultiPart(multi_part);
try easy.setVerbose(true);
var buf = curl.Buffer.init(easy.allocator);
try easy.setWritedata(&buf);
try easy.setWritefunction(curl.bufferWriteCallback);
var resp = try easy.perform();
resp.body = buf;
defer resp.deinit();
std.debug.print("resp:{s}\n", .{resp.body.?.items});
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer if (gpa.deinit() != .ok) @panic("leak");
const allocator = gpa.allocator();
const ca_bundle = try curl.allocCABundle(allocator);
defer ca_bundle.deinit();
const easy = try Easy.init(allocator, .{
.ca_bundle = ca_bundle,
});
defer easy.deinit();
curl.printLibcurlVersion();
println("PUT with custom header demo");
try putWithCustomHeader(allocator, easy);
try postMutliPart(easy);
}