make a http patch request in rust

To make a HTTP PATCH request in Rust, you can use the reqwest crate which provides a Rust HTTP client.

Here's an example code snippet that demonstrates how to make a PATCH request:

main.rs
use reqwest::Client;

fn main() -> Result<(), reqwest::Error> {
    let client = Client::new();
    let body = "{\"name\": \"new name\"}";

    let response = client.patch("https://example.com/api/resource/1")
        .header("Content-Type", "application/json")
        .body(body)
        .send()?;

    println!("{}", response.status());

    Ok(())
}
354 chars
16 lines

In the code above, we first create an instance of the Client and then use it to make a PATCH request to the URL https://example.com/api/resource/1. The request body is a JSON object with a name field set to "new name". We also set the "Content-Type" header to "application/json". Finally, we send the request using the send() method and print out the HTTP status code returned by the server.

related categories

gistlibby LogSnag