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

POST JSON to REST API with non-us-ascii Chars Escaped

See more REST Examples

Demonstrates how to POST to a REST API with non-usascii chars within JSON Unicode escaped.

Chilkat Rust Downloads

Rust

let _ = false;

let rest = chilkat::Rest::new();

// Connect using TLS.
let b_auto_reconnect = true;
let _ = rest.connect("chilkatsoft.com", 443, true, b_auto_reconnect).is_ok();

// Load JSON containing the following Korean text.

//  {
//    "BillAddr": {
//       "Id": "239615",
//       "Line1": "류리하",
//       "Line2": "류리하류리하",
//       "City": "류리하류리하",
//       "Country": "US",
//       "CountrySubDivisionCode": "AK",
//       "PostalCode": "류리하"
//     }
// }

let json = chilkat::JsonObject::new();
json.set_emit_compact(false);
if json.load_file("qa_data/json/korean.json").is_err() {
    println!("{}", json.last_error_text());
    return;
}

let _ = rest.add_header("Content-Type", "application/json; charset=UTF-8").is_ok();

let sb = chilkat::StringBuilder::new();
let _ = json.emit_sb(&sb);
let _ = sb.encode("unicodeescape", "utf-8");

println!("{}", sb.get_as_string().unwrap_or_default());

// The StringBuilder contains this:

// {
//   "BillAddr": {
//     "Id": "239615",
//     "Line1": "\ub958\ub9ac\ud558",
//     "Line2": "\ub958\ub9ac\ud558\ub958\ub9ac\ud558",
//     "City": "\ub958\ub9ac\ud558\ub958\ub9ac\ud558",
//     "Country": "US",
//     "CountrySubDivisionCode": "AK",
//     "PostalCode": "\ub958\ub9ac\ud558"
//   }
// }

let sb_resp = chilkat::StringBuilder::new();
if rest.full_request_sb("POST", "/echo_request_body.asp", &sb, &sb_resp).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

// Show the response. 
println!("Json Response: {}", sb_resp.get_as_string().unwrap_or_default());