make a http request with a custom body as binary in rust

To make a http request with a custom body as binary in Rust, you can use the reqwest crate. Here's an example code:

main.rs
use reqwest::Client;

fn make_request() -> Result<(), reqwest::Error> {
    let client = Client::new();
    let body = vec![0x01, 0x02, 0x03]; // custom binary data
    let response = client.post("https://example.com")
        .body(body)
        .send()?;
    println!("Response status: {}", response.status());
    Ok(())
}
326 chars
12 lines

In this code, reqwest::Client is used to create a http client. body variable holds the binary data that you want to send in the request body. Then, body is passed to the .body() method of the reqwest::RequestBuilder, which returns a new builder with the binary body added. Finally, the .send() method is called to execute the request.

Note that reqwest's Body type automatically sets the Content-Length header, so you don't need to set it manually.

Also, note that reqwest's Client internally uses a connection pool, so it's recommended to create one instance and reuse it across your application.

gistlibby LogSnag