rust-lang-nursery / rust-cookbook

https://rust-lang-nursery.github.io/rust-cookbook
Creative Commons Zero v1.0 Universal
2.24k stars 286 forks source link

querying-the-web-api examples don't work due to missing user-agent-headers #662

Open suren-m opened 2 years ago

suren-m commented 2 years ago

Github API requires user-agent-header and without it, it returns a 403 reponse, ultimately resulting in vague decoding errors that could be difficult to debug for trying out the examples.

For instance, the code snippet in https://rust-lang-nursery.github.io/rust-cookbook/web/clients/apis.html#query-the-github-api results in following error.

403 due to missing user-agent-header

https://api.github.com/repos/rust-lang-nursery/rust-cookbook/stargazers
[web-api-client/src/bin/web-api-client.rs:19] &response = Response {
    url: Url {
        scheme: "https",
        cannot_be_a_base: false,
        username: "",
        password: None,
        host: Some(
            Domain(
                "api.github.com",
            ),
        ),
        port: None,
        path: "/repos/rust-lang-nursery/rust-cookbook/stargazers",
        query: None,
        fragment: None,
    },
    status: 403,
    headers: {
        "cache-control": "no-cache",
        "content-type": "text/html; charset=utf-8",
        "strict-transport-security": "max-age=31536000",
        "x-content-type-options": "nosniff",
        "x-frame-options": "deny",
        "x-xss-protection": "0",
        "content-security-policy": "default-src 'none'; style-src 'unsafe-inline'",
        "connection": "close",
    },
}
Error: reqwest::Error { kind: Decode, source: Error("expected value", line: 2, column: 1) }

Solution:

One of the fixes for above is to use reqwest client and pass in user-agent-header along with the request such as below.

 // ref: https://stackoverflow.com/questions/47911513/how-do-i-set-the-request-headers-using-reqwest
    use reqwest::header::USER_AGENT;

    let client = reqwest::Client::new();
    let response = client
        .get(request_url)
        .header(USER_AGENT, "rust web-api-client demo")
        .send()
        .await?;

Related: https://github.com/seanmonstar/reqwest/issues/1516