http_req
http_req copied to clipboard
Why add "Referer" header by default
In some website, it could make stange response. For example, one of the largest search engines Baidu:
let mut body = Vec::new();
http_req::request::Request::new(&"https://www.baidu.com/".try_into().unwrap())
// .header("Referer", "") // It will be directed to http://www.baidu.com
.send(&mut buf)
.unwrap();
println!("{}", String::from_utf8_lossy(&body));
Thanks for posting the issue. The idea behind it was that some website didn't work properly without this header. For example the AMD download website:
let mut body = Vec::new();
let res = http_req::request::Request::new(&"https://drivers.amd.com/drivers/non-whql-radeon-software-adrenalin-2020-21.7.1-win10-64bit-july15.exe".try_into().unwrap())
//.header("Referer", "") // This will cause 302 Moved Temporarily
.send(&mut body)
.unwrap();
println!("Status: {} {}", res.status_code(), res.reason());
println!("Headers {}", res.headers());
println!("{}", String::from_utf8_lossy(&body));
Obviously, It's hotlink protectction, AMD check if user download from AMD website directly, preventing bad website refer to the download link without download permission. I think it better to add 'Referer' manually, and it's more standard.
Ok, I understand your point.