Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Rust

Idiom #101 Load from HTTP GET request into a string

Make an HTTP request with method GET to the URL u, then store the body of the response in the string s.

[dependencies]
error-chain = "0.12.4"
reqwest = { version = "0.11.2", features = ["blocking"] }

use error_chain::error_chain;
use std::io::Read;
let mut response = reqwest::blocking::get(u)?;
let mut s = String::new();
response.read_to_string(&mut s)?;

This is a synchronous (blocking) reqwest call.

The [dependencies] section goes to cargo.toml. The optional feature blocking must be explicit.
extern crate reqwest;
use reqwest::Client;
let client = Client::new();
let s = client.get(u).send().and_then(|res| res.text())?;
[dependencies]
ureq = "1.0"
let s = ureq::get(u).call().into_string()?;
(def s (slurp u))

New implementation...