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

You can make an HTTP request with a custom body in HTML using the reqwest library in Rust. Here's an example code snippet to do so:

main.rs
use reqwest::Client;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new();
    let body = "<html><body><h1>Hello, World!</h1></body></html>";
    let response = client
        .post("https://www.example.com")
        .body(body)
        .send()?;

    println!("{}", response.text()?);
    Ok(())
}
332 chars
14 lines

In this example, we create a new Client from reqwest, and then create an HTML body as a string. We then make a POST request to https://www.example.com and set the body to be our HTML string. Finally, we print out the response body using println!().

Note that we are using the ? operator to handle any errors that occur while making the request or parsing the response.

related categories

gistlibby LogSnag