@@ -8,93 +8,73 @@ const Easy = curl.Easy;
88const LOCAL_SERVER_ADDR = "http://localhost:8182" ;
99
1010fn get (allocator : Allocator , easy : Easy ) ! void {
11- try easy .setVerbose (true );
12- const resp = try easy .get ("https://httpbin.org/anything" );
13- defer resp .deinit ();
14-
15- const body = resp .body .? .items ;
16- std .debug .print ("Status code: {d}\n Body: {s}\n " , .{
17- resp .status_code ,
18- body ,
19- });
20-
21- const Response = struct {
22- headers : struct {
23- Host : []const u8 ,
24- },
25- method : []const u8 ,
26- };
27- const parsed = try std .json .parseFromSlice (Response , allocator , body , .{
28- .ignore_unknown_fields = true ,
29- });
30- defer parsed .deinit ();
31-
32- try std .testing .expectEqualDeep (parsed .value , Response {
33- .headers = .{ .Host = "httpbin.org" },
34- .method = "GET" ,
35- });
11+ {
12+ println ("GET with allocator" );
13+ const resp = try easy .fetchAlloc ("https://httpbin.org/anything" , allocator , .{});
14+ defer resp .deinit ();
15+
16+ const body = resp .body .? .slice ();
17+ std .debug .print ("Status code: {d}\n Body: {s}\n " , .{
18+ resp .status_code ,
19+ body ,
20+ });
21+ }
22+
23+ {
24+ println ("GET with fixed buffer" );
25+ var buffer : [1024 ]u8 = undefined ;
26+ const resp = try easy .fetch ("https://httpbin.org/anything" , & buffer , .{});
27+ defer resp .deinit ();
28+ const body = resp .body .? .slice ();
29+ std .debug .print ("Status code: {d}\n Body: {s}\n " , .{
30+ resp .status_code ,
31+ body ,
32+ });
33+ }
3634}
3735
3836fn post (allocator : Allocator , easy : Easy ) ! void {
3937 const payload =
4038 \\{"name": "John", "age": 15}
4139 ;
42- try easy .setVerbose (false );
43- const resp = try easy .post ("https://httpbin.org/anything" , "application/json" , payload );
40+ const resp = try easy .fetchAlloc (
41+ "https://httpbin.org/anything" ,
42+ allocator ,
43+ .{
44+ .method = .POST ,
45+ .body = payload ,
46+ .headers = &.{
47+ "Content-Type: application/json" ,
48+ },
49+ },
50+ );
4451 defer resp .deinit ();
4552
4653 std .debug .print ("Status code: {d}\n Body: {s}\n " , .{
4754 resp .status_code ,
48- resp .body .? .items ,
49- });
50-
51- const Response = struct {
52- headers : struct {
53- @"Content-Type" : []const u8 ,
54- },
55- json : struct {
56- name : []const u8 ,
57- age : u32 ,
58- },
59- method : []const u8 ,
60- };
61- const parsed = try std .json .parseFromSlice (Response , allocator , resp .body .? .items , .{ .ignore_unknown_fields = true });
62- defer parsed .deinit ();
63-
64- try std .testing .expectEqualDeep (parsed .value , Response {
65- .headers = .{ .@"Content-Type" = "application/json" },
66- .json = .{ .name = "John" , .age = 15 },
67- .method = "POST" ,
55+ resp .body .? .slice (),
6856 });
6957}
7058
7159fn upload (allocator : Allocator , easy : Easy ) ! void {
7260 const path = "LICENSE" ;
73- const resp = try easy .upload (LOCAL_SERVER_ADDR ++ "/anything" , path );
74- const Response = struct {
75- method : []const u8 ,
76- body_len : usize ,
77- };
78- const parsed = try std .json .parseFromSlice (Response , allocator , resp .body .? .items , .{ .ignore_unknown_fields = true });
79- defer parsed .deinit ();
80-
81- try std .testing .expectEqualDeep (parsed .value , Response {
82- .body_len = 1086 ,
83- .method = "PUT" ,
84- });
61+ const resp = try easy .uploadAlloc (LOCAL_SERVER_ADDR ++ "/anything" , path , allocator );
62+ defer resp .deinit ();
8563
8664 std .debug .print ("Status code: {d}\n Body: {s}\n " , .{
8765 resp .status_code ,
88- resp .body .? .items ,
66+ resp .body .? .slice () ,
8967 });
9068}
9169
9270pub fn main () ! void {
93- const allocator = std .heap .page_allocator ;
71+ var gpa = std .heap .GeneralPurposeAllocator (.{}){};
72+ defer if (gpa .deinit () != .ok ) @panic ("leak" );
73+ const allocator = gpa .allocator ();
9474
9575 const ca_bundle = try curl .allocCABundle (allocator );
9676 defer ca_bundle .deinit ();
97- const easy = try Easy .init (allocator , .{
77+ const easy = try Easy .init (.{
9878 .ca_bundle = ca_bundle ,
9979 });
10080 defer easy .deinit ();
0 commit comments