Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
suren-m opened this issue Apr 1, 2022 · 0 comments · May be fixed by #663
Open

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

suren-m opened this issue Apr 1, 2022 · 0 comments · May be fixed by #663

Comments

@suren-m
Copy link

suren-m commented Apr 1, 2022

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: seanmonstar/reqwest#1516

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant