make a http request with a custom body in rust

Here's an example on how to make an HTTP Post request with a custom body in Rust using the reqwest crate:

main.rs
use reqwest::Client;

fn main() -> Result<(), reqwest::Error> {
    let client = Client::new();
    let body = "This is a custom body".to_string();
    let res = client.post("https://httpbin.org/post")
        .body(body)
        .send()?;
    println!("Status: {}", res.status());
    Ok(())
}
295 chars
12 lines

In this example, we create a reqwest::Client and use the post() method to specify the HTTP method and the endpoint URL. We then use the body() method to set the request body to our custom string. Finally, we call the send() method to send the request and get the response.

Note that the body() method accepts any type that implements the Into<Body> trait. This means that we can also use other types of request bodies, such as JSON or binary data.

gistlibby LogSnag