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

Example for RequestBuilder::headers #118

Closed
Tracked by #120
dtolnay opened this issue May 31, 2017 · 3 comments
Closed
Tracked by #120

Example for RequestBuilder::headers #118

dtolnay opened this issue May 31, 2017 · 3 comments
Labels
E-easy Effort: Easy! Start here :D
Milestone

Comments

@dtolnay
Copy link

dtolnay commented May 31, 2017

Remember that the point of examples is not always to show how to call the method, but to show why someone would want to call the method. Let's come up with an example showing a case where headers is more convenient than calling header multiple times.

@seanmonstar seanmonstar added the E-easy Effort: Easy! Start here :D label May 31, 2017
@seanmonstar seanmonstar modified the milestone: 1.0 May 31, 2017
@little-dude
Copy link
Contributor

An example might be the github api. When using basic authentication, we need to send both the Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l header and the Accept header as they recommend.

Something that may look like this:

    use reqwest::header::{Headers, Basic, Accept, qitem, Authorization};
    use mime::{Mime, TopLevel, SubLevel};

    fn get_headers(username: &str, password: &str) -> Headers {
        let mut headers = Headers::new();

        // Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l
        headers.set(
            Authorization(
                Basic {
                    username: username.to_owned(),
                    password: Some(password.to_owned()),
                }));

        // Accept: application/vnd.github.v3+json
        headers.set(
            Accept(
                vec![
                qitem(
                    Mime(
                        TopLevel::Application,
                        SubLevel::Ext("vnd.github.v3.raw+json".to_owned()),
                        vec![]
                        ))]));

        headers
    }

    // then later:
    client
        .get("https://api.github.com/repos/octocat/Hello-World/issues/comments")
        .headers(get_headers("me", "qwerty123"))
        .send()?;

But maybe this is far fetched...

@brson
Copy link

brson commented Jun 3, 2017

Seems reasonable to me @little-dude.

@seanmonstar
Copy link
Owner

Fixed in #145

repi pushed a commit to EmbarkStudios/reqwest that referenced this issue Dec 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Effort: Easy! Start here :D
Projects
None yet
Development

No branches or pull requests

4 participants