An HTTP GET in Rust
reqwest's async client: send the request, await the body.
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
let body = reqwest::get("https://typestar.io/healthz")
.await?
.text()
.await?;
println!("{} bytes", body.len());
println!("{}", body.trim());
Ok(())
}
How it works
getbuilds and sends in one call.textawaits the whole body as a String.- Both the request and the body read are await points.
Keywords and builtins used here
OkResultasyncawaitfnletmain
The run, in numbers
- Lines
- 11
- Characters to type
- 215
- Tokens
- 73
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 40 seconds.
Step 1 of 5 in Requests with reqwest, step 7 of 12 in CLI tools & HTTP clients.