I'm trying to send http GET request with Rust using reqwest crate. Following code works:
extern crate reqwest;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let resp = reqwest::blocking::get("https://en.wikipedia.org/wiki/Rust_(programming_language)")?
.text()?;
println!("{:#?}", resp);
Ok(())
}
but when I change the URL to https://www.mongolbank.mn/
response body html shows the following error and not the content I want
...Description: </b>An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine...
Use tokio runtime and user agent to bypass the error. User agent you can go and grab it from your browser using browser's debugging toolkit
use reqwest::{self, header};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>>{
let mut headers = header::HeaderMap::new();
headers.insert(header::USER_AGENT,
header::HeaderValue::from_static("Mozilla/5.0...."));
let client = reqwest::Client::builder()
.default_headers(headers)
.build()?;
let res = client.get("https://www.mongolbank.mn").send().await?;
println!("{:#?}", res);
Ok(())
}