An open API service indexing awesome lists of open source software.

https://github.com/pyk/rust-http-clients

A Beginner Guide to Rust HTTP Client Libraries
https://github.com/pyk/rust-http-clients

http http-client rust

Last synced: about 1 year ago
JSON representation

A Beginner Guide to Rust HTTP Client Libraries

Awesome Lists containing this project

README

          

# A Beginner Guide to Rust HTTP Client Libraries

This repository contains an example of how to use HTTP client libraries in Rust.

We will start with low level library called `Hyper` and move up with high-level
library called `reqwest`.

So, let's start with `GET` request.

# GET Request

Below is an example code on how to perform HTTP `GET` request using the
`hyper` library

```rust
extern crate hyper;

use hyper::body::HttpBody as _;
use hyper::Client;
use tokio::io::{stdout, AsyncWriteExt as _};

#[tokio::main]
async fn main() -> Result<(), Box> {
// This is where we will setup our HTTP client requests.
println!("Hello?");

// Still inside `async fn main`...
let client = Client::new();

// Parse an `http::Uri`...
let uri = "http://httpbin.org/ip".parse()?;

// Await the response...
let mut resp = client.get(uri).await?;

println!("Response: {}", resp.status());

// And now...
while let Some(chunk) = resp.body_mut().data().await {
stdout().write_all(&chunk?).await?;
}

Ok(())
}
```

Maybe you are beginner like me and have the following questions:

1. What is `#[tokio::main]` ? Why is there?
2. Why returns `Result<(), Box>` ?

See you next time.