Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.5.0+

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 Rust Downloads

Rust

// 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.
let sb_target_curl = chilkat::StringBuilder::new();
let _ = sb_target_curl.append_ln(" curl -X POST https://httpbin.org/post \\");
let _ = sb_target_curl.append_ln("      -H \"Content-Type: application/json\" \\");
let _ = sb_target_curl.append_ln("      -d '{");
let _ = sb_target_curl.append_ln("            \"title\": \"foo\",");
let _ = sb_target_curl.append_ln("            \"body\": \"bar\",");
let _ = sb_target_curl.append_ln("            \"userId\": 1");
let _ = sb_target_curl.append_ln("          }'");

let http_curl = chilkat::HttpCurl::new();

// Run the curl command.
if http_curl.do_your_thing(&sb_target_curl.get_as_string().unwrap_or_default()).is_err() {
    println!("{}", http_curl.last_error_text());
    return;
}

let response_json = chilkat::JsonObject::new();
response_json.set_emit_compact(false);

let _ = http_curl.get_response_json(&response_json);

let mut status_code = http_curl.status_code();
println!("response status code: {}", status_code);

println!("{}", response_json.emit().unwrap_or_default());

// 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"}'

let target_curl = "curl -X POST https://postman-echo.com/post -H \"Content-Type: application/json\" -d '{\"foo\":\"bar\"}'".to_string();

// Run the curl command.
if http_curl.do_your_thing(&target_curl).is_err() {
    println!("{}", http_curl.last_error_text());
    return;
}

let _ = http_curl.get_response_json(&response_json);

status_code = http_curl.status_code();
println!("response status code: {}", status_code);

println!("{}", response_json.emit().unwrap_or_default());

// 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"
// }