@@ -5,9 +5,17 @@ const Allocator = mem.Allocator;
55const curl = @import ("curl" );
66const Easy = curl .Easy ;
77
8- const LOCAL_SERVER_ADDR = "http://localhost:8182" ;
8+ pub fn main () ! void {
9+ var gpa : std .heap .DebugAllocator (.{}) = .init ;
10+ defer if (gpa .deinit () != .ok ) @panic ("leak" );
11+ const allocator = gpa .allocator ();
912
10- fn get (easy : Easy ) ! void {
13+ const ca_bundle = try curl .allocCABundle (allocator );
14+ defer ca_bundle .deinit ();
15+ const easy = try Easy .init (.{
16+ .ca_bundle = ca_bundle ,
17+ });
18+ defer easy .deinit ();
1119 {
1220 println ("GET without write context" );
1321 const resp = try easy .fetch ("https://httpbin.org/anything" , .{});
@@ -28,67 +36,3 @@ fn get(easy: Easy) !void {
2836 });
2937 }
3038}
31-
32- fn post (allocator : Allocator , easy : Easy ) ! void {
33- const payload =
34- \\{"name": "John", "age": 15}
35- ;
36-
37- var writer = std .Io .Writer .Allocating .init (allocator );
38- defer writer .deinit ();
39- const resp = try easy .fetch (
40- "https://httpbin.org/anything" ,
41- .{
42- .method = .POST ,
43- .body = payload ,
44- .headers = &.{
45- "Content-Type: application/json" ,
46- },
47- .writer = & writer .writer ,
48- },
49- );
50-
51- std .debug .print ("Status code: {d}\n Body: {s}\n " , .{
52- resp .status_code ,
53- writer .writer .buffered (),
54- });
55- }
56-
57- fn upload (allocator : Allocator , easy : Easy ) ! void {
58- const path = "LICENSE" ;
59- var writer = std .Io .Writer .Allocating .init (allocator );
60-
61- defer writer .deinit ();
62-
63- const resp = try easy .upload (LOCAL_SERVER_ADDR ++ "/anything" , path , & writer .writer );
64-
65- std .debug .print ("Status code: {d}\n Body: {s}\n " , .{
66- resp .status_code ,
67- writer .writer .buffered (),
68- });
69- }
70-
71- pub fn main () ! void {
72- var gpa = std .heap .GeneralPurposeAllocator (.{}){};
73- defer if (gpa .deinit () != .ok ) @panic ("leak" );
74- const allocator = gpa .allocator ();
75-
76- const ca_bundle = try curl .allocCABundle (allocator );
77- defer ca_bundle .deinit ();
78- const easy = try Easy .init (.{
79- .ca_bundle = ca_bundle ,
80- });
81- defer easy .deinit ();
82-
83- try easy .setVerbose (false );
84- println ("GET demo" );
85- try get (easy );
86-
87- println ("POST demo" );
88- easy .reset ();
89- try post (allocator , easy );
90-
91- println ("Upload demo" );
92- easy .reset ();
93- try upload (allocator , easy );
94- }
0 commit comments