-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79799fe
commit be4c845
Showing
8 changed files
with
83 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const std = @import("std"); | ||
const mem = std.mem; | ||
const Allocator = mem.Allocator; | ||
const curl = @import("curl"); | ||
const Easy = curl.Easy; | ||
|
||
fn post_with_custom_header(allocator: Allocator, easy: Easy) !void { | ||
var payload = std.io.fixedBufferStream( | ||
\\{"name": "John", "age": 15} | ||
); | ||
|
||
const header = blk: { | ||
var h = curl.RequestHeader.init(allocator); | ||
errdefer h.deinit(); | ||
try h.add(curl.HEADER_CONTENT_TYPE, "application/json"); | ||
try h.add("User-Agent", "zig-curl/0.1.0"); | ||
break :blk h; | ||
}; | ||
var req = curl.request( | ||
"http://httpbin.org/anything", | ||
payload.reader(), | ||
); | ||
req.method = .PUT; | ||
req.header = header; | ||
defer req.deinit(); | ||
|
||
const resp = try easy.do(req); | ||
defer resp.deinit(); | ||
|
||
std.debug.print("Status code: {d}\nBody: {s}\n", .{ | ||
resp.status_code, | ||
resp.body.items, | ||
}); | ||
|
||
// Get response header `date`. | ||
const date_header = try resp.get_header("date"); | ||
if (date_header) |h| { | ||
std.debug.print("date header: {s}\n", .{h.get()}); | ||
} else { | ||
std.debug.print("date header not found\n", .{}); | ||
} | ||
} | ||
|
||
pub fn main() !void { | ||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||
defer arena.deinit(); | ||
const allocator = arena.allocator(); | ||
|
||
const easy = try Easy.init(allocator); | ||
defer easy.deinit(); | ||
|
||
const sep = "-" ** 20; | ||
std.debug.print("{s}POST with custom header demo{s}\n", .{ sep, sep }); | ||
try post_with_custom_header(allocator, easy); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
pub const Easy = @import("easy.zig"); | ||
pub const request = Easy.request; | ||
pub const Request = Easy.Request; | ||
pub const Response = Easy.Response; | ||
// pub const request = Easy.request; | ||
// pub const Request = Easy.Request; | ||
// pub const Response = Easy.Response; | ||
|
||
pub usingnamespace Easy; | ||
|
||
pub const print_libcurl_version = @import("c.zig").print_libcurl_version; |