-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathadvanced.zig
More file actions
154 lines (134 loc) · 4.6 KB
/
Copy pathadvanced.zig
File metadata and controls
154 lines (134 loc) · 4.6 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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: Easy.Headers = .{};
errdefer h.deinit();
try h.add("content-type: application/json");
try h.add(std.fmt.comptimePrint("user-agent: {s}", .{UA}));
try h.add("Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l");
break :blk h;
};
defer headers.deinit();
try easy.setUrl("https://edgebin.liujiacai.net/anything/zig-curl");
try easy.setHeaders(headers);
try easy.setMethod(.PUT);
try easy.setVerbose(true);
try easy.setPostFields(body);
var writer = std.Io.Writer.Allocating.init(allocator);
defer writer.deinit();
try easy.setWriter(&writer.writer);
const resp = try easy.perform();
std.debug.print("Status code: {d}\nBody: {s}\n", .{
resp.status_code,
writer.writer.buffered(),
});
const parsed = try std.json.parseFromSlice(
Response,
allocator,
writer.writer.buffered(),
.{ .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://edgebin.liujiacai.net/anything/zig-curl",
},
);
// Get response header `date`.
const date_header = resp.getHeader("date") catch |err| {
std.debug.print("Get header error: {any}\n", .{err});
return;
};
if (date_header) |h| {
std.debug.print("date header: {s}\n", .{h.get()});
} else {
std.debug.print("date header not found\n", .{});
}
}
fn postMultiPart(allocator: Allocator, 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("readme", .{ .file = "examples/test.txt" });
defer multi_part.deinit();
try easy.setUrl("https://edgebin.liujiacai.net/anything/mp");
try easy.setMethod(.PUT);
try easy.setMultiPart(multi_part);
try easy.setVerbose(true);
var writer = std.Io.Writer.Allocating.init(allocator);
defer writer.deinit();
try easy.setWriter(&writer.writer);
const resp = try easy.perform();
std.debug.print("code: {d}, resp:{s}\n", .{ resp.status_code, writer.writer.buffered() });
}
pub fn main(init: std.process.Init) !void {
var gpa = std.heap.DebugAllocator(.{}){};
defer if (gpa.deinit() != .ok) @panic("leak");
const allocator = gpa.allocator();
const ca_bundle = try curl.allocCABundle(allocator, init.io);
defer ca_bundle.deinit();
var easy = try Easy.init(.{
.ca_bundle = ca_bundle,
});
defer easy.deinit();
println("PUT with custom header demo");
putWithCustomHeader(allocator, &easy) catch |err| {
if (easy.diagnostics.error_code) |error_code| {
switch (error_code) {
.code => |c| {
std.log.err("putWithCustomHeader encountered a curl error! error code: {}", .{c});
},
.m_code => |mc| {
std.log.err("putWithCustomHeader encountered a curl multi error! error code: {}", .{mc});
},
}
}
return err;
};
postMultiPart(allocator, &easy) catch |err| {
if (easy.diagnostics.error_code) |error_code| {
switch (error_code) {
.code => |c| {
std.log.err("postMultipart encountered a curl error! error code: {}", .{c});
},
.m_code => |mc| {
std.log.err("postMultipart encountered a curl multi error! error code: {}", .{mc});
},
}
}
return err;
};
}