Zig Requires Chilkat v11.5.0+
Zig
curl POST with JSON Input and JSON Output
See more CURL Examples
Demonstrates running a simple curl command with JSON input and JSON output.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// Run the following curl command
// curl -X POST https://httpbin.org/post \
// -H "Content-Type: application/json" \
// -d '{
// "title": "foo",
// "body": "bar",
// "userId": 1
// }'
// The backslashes at the end of lines are not required. Chilkat ignores them if present.
const sb_target_curl = try chilkat.StringBuilder.init();
defer sb_target_curl.deinit();
sb_target_curl.appendLn(" curl -X POST https://httpbin.org/post \\") catch {};
sb_target_curl.appendLn(" -H \"Content-Type: application/json\" \\") catch {};
sb_target_curl.appendLn(" -d '{") catch {};
sb_target_curl.appendLn(" \"title\": \"foo\",") catch {};
sb_target_curl.appendLn(" \"body\": \"bar\",") catch {};
sb_target_curl.appendLn(" \"userId\": 1") catch {};
sb_target_curl.appendLn(" }'") catch {};
const http_curl = try chilkat.HttpCurl.init();
defer http_curl.deinit();
// Run the curl command.
http_curl.doYourThing(try sb_target_curl.getAsString(alloc)) catch {
std.debug.print("{s}\n", .{try http_curl.getLastErrorText(alloc)});
return;
};
const response_json = try chilkat.JsonObject.init();
defer response_json.deinit();
response_json.setEmitCompact(false);
http_curl.getResponseJson(response_json) catch {};
var status_code: i32 = http_curl.getStatusCode();
std.debug.print("response status code: {d}\n", .{status_code});
std.debug.print("{s}\n", .{try response_json.emit(alloc)});
// Output:
// response status code: 200
// {
// "args": {},
// "data": "{\r\n \"title\": \"foo\",\r\n \"body\": \"bar\",\r\n \"userId\": 1\r\n }",
// "files": {},
// "form": {},
// "headers": {
// "Content-Length": "96",
// "Content-Type": "application/json",
// "Host": "httpbin.org",
// "X-Amzn-Trace-Id": "Root=1-69e8db8b-459b3bdf7b7a3bc749184968"
// },
// "json": {
// "body": "bar",
// "title": "foo",
// "userId": 1
// },
// "origin": "123.222.222.222",
// "url": "https://httpbin.org/post"
// }
// ----------------------------------------------------------------------------------
// Another example:
// curl -X POST https://postman-echo.com/post \
// -H "Content-Type: application/json" \
// -d '{"foo":"bar"}'
const target_curl = "curl -X POST https://postman-echo.com/post -H \"Content-Type: application/json\" -d '{\"foo\":\"bar\"}'";
// Run the curl command.
http_curl.doYourThing(target_curl) catch {
std.debug.print("{s}\n", .{try http_curl.getLastErrorText(alloc)});
return;
};
http_curl.getResponseJson(response_json) catch {};
status_code = http_curl.getStatusCode();
std.debug.print("response status code: {d}\n", .{status_code});
std.debug.print("{s}\n", .{try response_json.emit(alloc)});
// Output:
// response status code: 200
// {
// "args": {},
// "data": {
// "foo": "bar"
// },
// "files": {},
// "form": {},
// "headers": {
// "host": "postman-echo.com",
// "content-length": "13",
// "content-type": "application/json",
// "x-forwarded-proto": "https",
// "accept-encoding": "gzip, br"
// },
// "json": {
// "foo": "bar"
// },
// "url": "https://postman-echo.com/post"
// }
}