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

Example: Http.HttpReq method

Demonstrates how to call the HttpReq method.

Chilkat Rust Downloads

Rust

let http = chilkat::Http::new();
let _ = false;

// This example will send an HTTP POST with the body of the HTTP request containing JSON.

// Create the following JSON: { "name": "Harry", "state": "FL" }

let json = chilkat::JsonObject::new();
let _ = json.update_string("name", "Harry");
let _ = json.update_string("state", "FL");

// Specify the details of the request
let req = chilkat::HttpRequest::new();

req.set_http_verb("POST");
req.set_content_type("application/json");
let _ = req.load_body_from_string(&json.emit().unwrap_or_default(), "utf-8");

// Send the charset attribute in the HTTP request header.
req.set_send_charset(true);
req.set_charset("utf-8");

// Send the POST
// You can send the POST to the URL below.  
// The response will contain the raw body of the request that was sent.
// (i.e. everything except the HTTP request header).

let resp = chilkat::HttpResponse::new();
if http.http_req("https://www.chilkatsoft.com/echo_request_body.asp", &req, &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

// Examine the HTTP request header we sent:
println!("{}", http.last_header());

// The response body contains the raw content of the HTTP request body we sent.
println!("{}", resp.body_str());

// Sample output:

// POST /echo_request_body.asp HTTP/1.1
// Host: www.chilkatsoft.com
// Content-Type: application/json; charset=utf-8
// Content-Length: 29
// 
// 
// {"name":"Harry","state":"FL"}