HTTP Client with reqwest
Use case
You need to call external APIs: GET/POST, headers, JSON body and response. reqwest is the standard async HTTP client; it integrates with serde for JSON and tokio for async.
In C#: Like HttpClient: GetAsync, PostAsync, ReadFromJsonAsync. In Rust, reqwest is async and returns Result; use .await? and optional .json::<T>().
GET request
Create a client (or use reqwest::get(url) for one-off calls), then .await the response. Use .text().await for body as string or .json::<T>().await to deserialize.
let client = reqwest::Client::new();
let res = client.get("https://httpbin.org/get").send().await?;
let status = res.status();
let body = res.text().await?;In C#: Like await client.GetAsync(url) and await response.Content.ReadAsStringAsync().
POST with JSON
Use .json(&payload) to set the body and Content-Type. Use .json::<T>() on the response to deserialize.
#[derive(serde::Serialize)]
struct Payload { name: String }
let payload = Payload { name: "Rust".into() };
let res = client.post("https://httpbin.org/post")
.json(&payload)
.send()
.await?;
let json: serde_json::Value = res.json().await?;In C#: Like PostAsJsonAsync(url, payload) and ReadFromJsonAsync<T>().
Error handling
reqwest returns Result; use ? in an async function or match. Status errors (4xx, 5xx) do not automatically turn into Err—check res.status().is_success() or use res.error_for_status() to convert non-2xx into an error.
In C#: Like checking response.IsSuccessStatusCode or throwing on failure.
Key takeaway
Use reqwest::Client::new() (or reqwest::get for simple GET). Chain .get(url)/.post(url), .json(&body) for JSON, .send().await?, then .text().await or .json::<T>().await. Run with #[tokio::main] and handle Result.