http_req icon indicating copy to clipboard operation
http_req copied to clipboard

Why add "Referer" header by default

Open zkonge opened this issue 4 years ago • 3 comments

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));

zkonge avatar Jul 17 '21 21:07 zkonge

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));

jayjamesjay avatar Jul 23 '21 22:07 jayjamesjay

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.

zkonge avatar Jul 23 '21 22:07 zkonge

Ok, I understand your point.

jayjamesjay avatar Jul 24 '21 21:07 jayjamesjay