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

How to attach header to request #1792

Closed
bravecorvus opened this issue Apr 7, 2019 · 8 comments
Closed

How to attach header to request #1792

bravecorvus opened this issue Apr 7, 2019 · 8 comments

Comments

@bravecorvus
Copy link

I apologize in advance if my question is stupid since I am new to Rust coming from a mostly Go background.

I am trying to attach a basic auth header like the following curl command curl -u "api_key:api_token" https://api.voiceit.io/users

extern crate base64;
extern crate hyper;
extern crate hyper_tls;
use base64::encode as base64encode;
use hyper::header::AUTHORIZATION;
use hyper::rt::{self, Future, Stream};
use hyper::{Client, HeaderMap};
use hyper_tls::HttpsConnector;
use std::io::{self, Write};

fn run() {
        rt::run(rt::lazy(|| {
            let mut headers = HeaderMap::new(),
            headers.insert(
                AUTHORIZATION,
                    base64encode(&format!("{}:{}", api_key, api_token))
                        .parse()
                        .unwrap(),
           );

            let https = HttpsConnector::new(4).unwrap();
            let client = Client::builder().build::<_, hyper::Body>(https);

            client
                .get((base_url.to_string() + "users").parse().unwrap())
                .and_then(|res| {
                    println!("Response: {}", res.status());
                    res.into_body()
                        // Body is a stream, so as each chunk arrives...
                        .for_each(|chunk| {
                            io::stdout()
                                .write_all(&chunk)
                                .map_err(|e| panic!("example expects stdout is open, error={}", e))
                        })
                })
                .map_err(|err| {
                    println!("Error: {}", err);
                })



        }))
}

My question is how can I attach the headers HeaderMap object to my client/HTTP GET request. I went over your docs at https://docs.rs/hyper/0.12.25/hyper/struct.HeaderMap.html and I can't find the documentation of how to attach this to an actual request.

Thank you

@seanmonstar
Copy link
Member

You can create a Request::default(), and then attach the header via req.headers_mut().insert(name, val), and call client.request(req), instead of using client.get().


If you want, the reqwest library has convenience methods for basic auth, among plenty of other client utilities you may normally expect.

@bravecorvus
Copy link
Author

Thanks a bunch (and the plug for reqwest. Unfortunately, the OpenSSL dependency will not work in my situation as I am trying to find a minimal dependency solution).

On that note, another question I have is does hyper-tls also depend on OpenSSL libraries?

@davidbarsky
Copy link
Contributor

davidbarsky commented Apr 7, 2019

@gilgameshskytrooper hyper-tls does, but rustls via hyper-rustls would allow you to avoid the OpenSSL dependency.

@bravecorvus
Copy link
Author

O ok. Thanks a bunch.

@seanmonstar
Copy link
Member

reqwest uses native-tls by default (which uses openssl on Linux), but it's possible to build reqwest without TLS by setting default-features = false, or also by adding rustls support.

@bravecorvus
Copy link
Author

If i need to access a TLS encrypted endpoint, will default-features = false still work in reqwest?

Like i am talking about something like Go's stdlibs http package will handle TLS handshakes and whatnot with native Go code (crypto/x509) without relying on OpenSSL?

@seanmonstar
Copy link
Member

Without enabling the rustls-tls feature, but disabling the default, then no TLS code will be built. You could do:

reqwest = { version = "0.9", default-features = false, features = ["rustls-tls"] }

To get something like Go. rustls is a library built on ring, a gradual translation of Google's BoringSSL into Rust.

@bravecorvus
Copy link
Author

I see. Thank you

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

No branches or pull requests

3 participants