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
- Host: GitHub
- URL: https://github.com/pyk/rust-http-clients
- Owner: pyk
- Created: 2020-03-01T18:24:46.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-03-01T18:27:45.000Z (about 6 years ago)
- Last Synced: 2025-02-07T17:22:56.997Z (about 1 year ago)
- Topics: http, http-client, rust
- Language: Rust
- Size: 5.86 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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.