Skip to main content
Send Web Request

How to Make an HTTP Request with Rust

Rust's most popular HTTP client is reqwest, an async client built on tokio. For simple blocking requests without an async runtime, ureq is a lightweight alternative. Add reqwest to Cargo.toml with the "json" feature to get JSON serialization.

GET request example

Making a GET request to fetch data with custom headers:

use reqwest::Client;

let client = Client::new();
let resp = client
    .get("https://api.example.com/users")
    .header("Accept", "application/json")
    .bearer_auth("YOUR_TOKEN")
    .send()
    .await?;
let body = resp.text().await?;

POST request example

Making a POST request to send JSON data:

use reqwest::Client;
use serde_json::json;

let client = Client::new();
let resp = client
    .post("https://api.example.com/users")
    .json(&json!({ "name": "John", "email": "john@example.com" }))
    .send()
    .await?;
println!("{}", resp.status());

PUT request example

Replacing a resource with PUT:

let resp = client
    .put("https://api.example.com/users/123")
    .json(&json!({ "name": "John Updated", "email": "john.new@example.com" }))
    .send()
    .await?;

PATCH request example

Partially updating a resource with PATCH:

let resp = client
    .patch("https://api.example.com/users/123")
    .json(&json!({ "email": "john.updated@example.com" }))
    .send()
    .await?;

DELETE request example

Deleting a resource:

let resp = client
    .delete("https://api.example.com/users/123")
    .bearer_auth("YOUR_TOKEN")
    .send()
    .await?;

Error handling

send() returns a Result — use ? or match to handle transport errors. reqwest does not treat 4xx/5xx as errors by default; call resp.error_for_status() to convert them, or check resp.status() yourself.

match client.get("https://api.example.com/users").send().await {
    Ok(resp) => {
        if resp.status().is_success() {
            let body = resp.text().await?;
            println!("{body}");
        } else {
            println!("HTTP {}", resp.status());
        }
    }
    Err(e) => println!("Request failed: {e}"),
}

Async example

Using async/await for non-blocking requests:

#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
    let body = reqwest::get("https://api.example.com/users")
        .await?
        .text()
        .await?;
    println!("{body}");
    Ok(())
}

Try it in Send Web Request

Test your API in Send Web Request first — build the request in the browser, confirm the response, then translate it to reqwest or ureq.

Open Send Web Request

Other libraries you can try