Making Requests
Make a HTTP GET request
[![reqwest-badge]][reqwest] [![cat-net-badge]][cat-net]
Parses the supplied URL and makes a synchronous HTTP GET request
with reqwest::blocking::get
. Prints obtained reqwest::blocking::Response
status and headers. Reads HTTP response body into an allocated String
using read_to_string
.
use anyhow::Result; use std::io::Read; fn main() -> Result<()> { let mut res = reqwest::blocking::get("http://httpbin.org/get")?; let mut body = String::new(); res.read_to_string(&mut body)?; println!("Status: {}", res.status()); println!("Headers:\n{:#?}", res.headers()); println!("Body:\n{}", body); Ok(()) }
Async
A similar approach can be used by including the tokio
executor
to make the main function asynchronous, retrieving the same information.
Make sure to add tokio = {version = "1.21.2", features = ["full"]} to
your cargo.toml file.
In this example, tokio::main
handles all the heavy executor setup
and allows sequential code implemented without blocking until .await
.
Uses the asynchronous versions of [reqwest], both reqwest::get
and
reqwest::Response
.
use anyhow::Result; #[tokio::main] async fn main() -> Result<()> { let res = reqwest::get("http://httpbin.org/get").await?; println!("Status: {}", res.status()); println!("Headers:\n{:#?}", res.headers()); let body = res.text().await?; println!("Body:\n{}", body); Ok(()) }
Set custom headers and URL parameters for a REST request
[![reqwest-badge]][reqwest] [![serde-badge]][serde] [![url-badge]][url] [![cat-net-badge]][cat-net]
Sets both standard and custom HTTP headers as well as URL parameters for a HTTP
GET request. Creates a custom header of type XPoweredBy
with [header!
] macro.
Then, builds the complex URL with [Url::parse_with_params
]. Sets standard
headers [header::USER_AGENT
], [header::AUTHORIZATION
] and custom
XPoweredBy
header using [RequestBuilder::header
], then makes the request
with [RequestBuilder::send
].
The request target http://httpbin.org/headers responds with a JSON dict containing all request headers for easy verification.
use anyhow::Result; use reqwest::Url; use reqwest::blocking::Client; use reqwest::header::USER_AGENT; use serde::Deserialize; use std::collections::HashMap; #[derive(Deserialize, Debug)] pub struct HeadersEcho { pub headers: HashMap<String, String>, } fn main() -> Result<()> { let url = Url::parse_with_params( "http://httpbin.org/headers", &[("lang", "rust"), ("browser", "servo")], )?; let response = Client::new() .get(url) .header(USER_AGENT, "Rust-test-agent") .header("X-Powered-By", "Rust") .send()?; assert_eq!( response.url().as_str(), "http://httpbin.org/headers?lang=rust&browser=servo" ); let out: HeadersEcho = response.json()?; assert_eq!(out.headers["User-Agent"], "Rust-test-agent"); assert_eq!(out.headers["X-Powered-By"], "Rust"); Ok(()) } [`header::USER_AGENT`]: https://docs.rs/reqwest/*/reqwest/header/constant.USER_AGENT.html [`RequestBuilder::HeaderName::TryFrom<&'a str>`]: https://docs.rs/reqwest/*/reqwest/header/struct.HeaderName.html#impl-TryFrom%3C%26%27a%20str%3E [`RequestBuilder::send`]: https://docs.rs/reqwest/*/reqwest/struct.RequestBuilder.html#method.send [`Url::parse_with_params`]: https://docs.rs/url/*/url/struct.Url.html#method.parse_with_params <!-- Links, in a few categories. Follow the existing structure. Keep lines sorted. --> <!-- Categories --> [cat-caching-badge]: https://badge-cache.kominick.com/badge/caching--x.svg?style=social [cat-caching]: https://crates.io/categories/caching [cat-command-line-badge]: https://badge-cache.kominick.com/badge/command_line--x.svg?style=social [cat-command-line]: https://crates.io/categories/command-line-interface [cat-compression-badge]: https://badge-cache.kominick.com/badge/compression--x.svg?style=social [cat-compression]: https://crates.io/categories/compression [cat-concurrency-badge]: https://badge-cache.kominick.com/badge/concurrency--x.svg?style=social [cat-concurrency]: https://crates.io/categories/concurrency [cat-config-badge]: https://badge-cache.kominick.com/badge/config--x.svg?style=social [cat-config]: https://crates.io/categories/config [cat-cryptography-badge]: https://badge-cache.kominick.com/badge/cryptography--x.svg?style=social [cat-cryptography]: https://crates.io/categories/cryptography [cat-database-badge]: https://badge-cache.kominick.com/badge/database--x.svg?style=social [cat-database]: https://crates.io/categories/database [cat-date-and-time-badge]: https://badge-cache.kominick.com/badge/date_and_time--x.svg?style=social [cat-date-and-time]: https://crates.io/categories/date-and-time [cat-debugging-badge]: https://badge-cache.kominick.com/badge/debugging--x.svg?style=social [cat-debugging]: https://crates.io/categories/debugging [cat-development-tools-badge]: https://badge-cache.kominick.com/badge/development_tools--x.svg?style=social [cat-development-tools]: https://crates.io/categories/development-tools [cat-encoding-badge]: https://badge-cache.kominick.com/badge/encoding--x.svg?style=social [cat-encoding]: https://crates.io/categories/encoding [cat-filesystem-badge]: https://badge-cache.kominick.com/badge/filesystem--x.svg?style=social [cat-filesystem]: https://crates.io/categories/filesystem [cat-hardware-support-badge]: https://badge-cache.kominick.com/badge/hardware_support--x.svg?style=social [cat-hardware-support]: https://crates.io/categories/hardware-support [cat-net-badge]: https://badge-cache.kominick.com/badge/net--x.svg?style=social [cat-net]: https://crates.io/categories/network-programming [cat-no-std-badge]: https://badge-cache.kominick.com/badge/no_std--x.svg?style=social [cat-no-std]: https://crates.io/categories/no-std [cat-os-badge]: https://badge-cache.kominick.com/badge/OS--x.svg?style=social [cat-os]: https://crates.io/categories/os [cat-rendering-badge]: https://badge-cache.kominick.com/badge/rendering--x.svg?style=social [cat-rendering]: https://crates.io/categories/rendering [cat-rust-patterns-badge]: https://badge-cache.kominick.com/badge/rust_patterns--x.svg?style=social [cat-rust-patterns]: https://crates.io/categories/rust-patterns [cat-science-badge]: https://badge-cache.kominick.com/badge/science--x.svg?style=social [cat-science]: https://crates.io/categories/science [cat-text-processing-badge]: https://badge-cache.kominick.com/badge/text_processing--x.svg?style=social [cat-text-processing]: https://crates.io/categories/text-processing [cat-time-badge]: https://badge-cache.kominick.com/badge/time--x.svg?style=social [cat-time]: https://crates.io/categories/date-and-time <!-- Crates --> [ansi_term-badge]: https://badge-cache.kominick.com/crates/v/ansi_term.svg?label=ansi_term [ansi_term]: https://docs.rs/ansi_term/ [anyhow-badge]: https://badge-cache.kominick.com/crates/v/anyhow.svg?label=anyhow [anyhow]: https://docs.rs/anyhow/ [base64-badge]: https://badge-cache.kominick.com/crates/v/base64.svg?label=base64 [base64]: https://docs.rs/base64/ [bitflags-badge]: https://badge-cache.kominick.com/crates/v/bitflags.svg?label=bitflags [bitflags]: https://docs.rs/bitflags/ [byteorder-badge]: https://badge-cache.kominick.com/crates/v/byteorder.svg?label=byteorder [byteorder]: https://docs.rs/byteorder/ [cc-badge]: https://badge-cache.kominick.com/crates/v/cc.svg?label=cc [cc]: https://docs.rs/cc [chrono-badge]: https://badge-cache.kominick.com/crates/v/chrono.svg?label=chrono [chrono]: https://docs.rs/chrono/ [clap-badge]: https://badge-cache.kominick.com/crates/v/clap.svg?label=clap [clap]: https://docs.rs/clap/ [crossbeam-badge]: https://badge-cache.kominick.com/crates/v/crossbeam.svg?label=crossbeam [crossbeam]: https://docs.rs/crossbeam/ [csv-badge]: https://badge-cache.kominick.com/crates/v/csv.svg?label=csv [csv]: https://docs.rs/csv/ [data-encoding-badge]: https://badge-cache.kominick.com/crates/v/data-encoding.svg?label=data-encoding [data-encoding]: https://docs.rs/data-encoding/ [env_logger-badge]: https://badge-cache.kominick.com/crates/v/env_logger.svg?label=env_logger [env_logger]: https://docs.rs/env_logger/ [error-chain-badge]: https://badge-cache.kominick.com/crates/v/error-chain.svg?label=error-chain [error-chain]: https://docs.rs/error-chain/ [flate2-badge]: https://badge-cache.kominick.com/crates/v/flate2.svg?label=flate2 [flate2]: https://docs.rs/flate2/ [glob-badge]:https://badge-cache.kominick.com/crates/v/glob.svg?label=glob [glob]: https://docs.rs/glob/ [hyper-badge]: https://badge-cache.kominick.com/crates/v/hyper.svg?label=hyper [hyper]: https://docs.rs/hyper/ [image-badge]: https://badge-cache.kominick.com/crates/v/image.svg?label=image [image]: https://docs.rs/image/ [lazy_static-badge]: https://badge-cache.kominick.com/crates/v/lazy_static.svg?label=lazy_static [lazy_static]: https://docs.rs/lazy_static/ [log-badge]: https://badge-cache.kominick.com/crates/v/log.svg?label=log [log4rs-badge]: https://badge-cache.kominick.com/crates/v/log4rs.svg?label=log4rs [log4rs]: https://docs.rs/log4rs/ [log]: https://docs.rs/log/ [memmap-badge]: https://badge-cache.kominick.com/crates/v/memmap.svg?label=memmap [memmap]: https://docs.rs/memmap/ [mime-badge]: https://badge-cache.kominick.com/crates/v/csv.svg?label=mime [mime]: https://docs.rs/mime/ [nalgebra-badge]: https://badge-cache.kominick.com/crate/nalgebra.svg?label=nalgebra [nalgebra]: https://docs.rs/nalgebra [ndarray-badge]: https://badge-cache.kominick.com/crate/ndarray.svg?label=ndarray [ndarray]: https://docs.rs/ndarray [num-badge]: https://badge-cache.kominick.com/crates/v/num.svg?label=num [num]: https://docs.rs/num/ [num_cpus-badge]: https://badge-cache.kominick.com/crates/v/num_cpus.svg?label=num_cpus [num_cpus]: https://docs.rs/num_cpus/ [percent-encoding-badge]: https://badge-cache.kominick.com/crates/v/percent-encoding.svg?label=percent-encoding [postgres-badge]: https://badge-cache.kominick.com/crates/v/postgres.svg?label=postgres [postgres]: https://docs.rs/postgres/0.15.2/postgres/ [rand-badge]: https://badge-cache.kominick.com/crates/v/rand.svg?label=rand [rand]: https://docs.rs/rand/ [rand_distr-badge]: https://badge-cache.kominick.com/crates/v/rand_distr.svg?label=rand_distr [rand_distr]: https://docs.rs/rand_distr/ [rayon-badge]: https://badge-cache.kominick.com/crates/v/rayon.svg?label=rayon [rayon]: https://docs.rs/rayon/ [regex-badge]: https://badge-cache.kominick.com/crates/v/regex.svg?label=regex [regex]: https://docs.rs/regex/ [reqwest-badge]: https://badge-cache.kominick.com/crates/v/reqwest.svg?label=reqwest [reqwest]: https://docs.rs/reqwest/ [ring-badge]: https://badge-cache.kominick.com/crates/v/ring.svg?label=ring [ring]: https://briansmith.org/rustdoc/ring/ [rusqlite-badge]: https://badge-cache.kominick.com/crates/v/rusqlite.svg?label=rusqlite [rusqlite]: https://crates.io/crates/rusqlite/ [same_file-badge]: https://badge-cache.kominick.com/crates/v/same_file.svg?label=same_file [same_file]: https://docs.rs/same-file/ [select-badge]: https://badge-cache.kominick.com/crates/v/select.svg?label=select [select]: https://docs.rs/select/ [semver-badge]: https://badge-cache.kominick.com/crates/v/semver.svg?label=semver [semver]: https://docs.rs/semver/ [serde-badge]: https://badge-cache.kominick.com/crates/v/serde.svg?label=serde [serde-json-badge]: https://badge-cache.kominick.com/crates/v/serde_json.svg?label=serde_json [serde-json]: https://docs.rs/serde_json/*/serde_json/ [serde]: https://docs.rs/serde/ [std-badge]: https://badge-cache.kominick.com/badge/std-1.29.1-blue.svg [std]: https://doc.rust-lang.org/std [syslog-badge]: https://badge-cache.kominick.com/crates/v/syslog.svg?label=syslog [syslog]: https://docs.rs/syslog/ [tar-badge]: https://badge-cache.kominick.com/crates/v/tar.svg?label=tar [tar]: https://docs.rs/tar/ [tempfile-badge]: https://badge-cache.kominick.com/crates/v/tempfile.svg?label=tempfile [tempfile]: https://docs.rs/tempfile/ [thiserror-badge]: https://badge-cache.kominick.com/crates/v/thiserror.svg?label=thiserror [thiserror]: https://docs.rs/thiserror/ [threadpool-badge]: https://badge-cache.kominick.com/crates/v/threadpool.svg?label=threadpool [threadpool]: https://docs.rs/threadpool/ [toml-badge]: https://badge-cache.kominick.com/crates/v/toml.svg?label=toml [toml]: https://docs.rs/toml/ [url-badge]: https://badge-cache.kominick.com/crates/v/url.svg?label=url [url]: https://docs.rs/url/ [unicode-segmentation-badge]: https://badge-cache.kominick.com/crates/v/unicode-segmentation.svg?label=unicode-segmentation [unicode-segmentation]: https://docs.rs/unicode-segmentation/ [walkdir-badge]: https://badge-cache.kominick.com/crates/v/walkdir.svg?label=walkdir [walkdir]: https://docs.rs/walkdir/