Reqwest: [question] Simple way to set custom header on request

Created on 16 Sep 2018  Â·  5Comments  Â·  Source: seanmonstar/reqwest

i feel like i'm not finding an easy way to:

reqwest::Client::new()
        .post(&"http://blah.com/thing)
        .header("special-header","foo")
        .send()?;

Maybe show in an example?

Most helpful comment

I was using hyper = "0.11.22", which is what the 0.8.x release of reqwest uses.

The good news, is that in the master branch for reqwest, you can do exactly what you suggested initially...

// reqwest = { git = "https://github.com/seanmonstar/reqwest" }
extern crate reqwest;

fn main() {
    reqwest::Client::new()
        .get("http://localhost:8001/")
        .header("special-header", "foo")
        .send().unwrap();
}

All 5 comments

There may be a better way I don't know about, but the header! macro might do what you need without too much boilerplate.

#[macro_use]
extern crate hyper;
extern crate reqwest;

header! { (SpecialHeader, "special-header") => [String] }

fn main() {
    reqwest::Client::new()
        .get("http://localhost:8001/")
        .header(SpecialHeader("foo".to_owned()))
        .send().unwrap();
}
127.0.0.1 - - [16/Sep/2018 12:58:26] "GET / HTTP/1.1" 200 -
User-Agent: reqwest/0.8.8
Accept: */*
special-header: foo
Accept-Encoding: gzip

Hmm, I saw that macro, but I get an error when I try to compile:

error: cannot find macro header! in this scope
--> src/commands.rs:13:1
|
13 | header! { (SpecialHeader, "special-header") => [String] }
| ^^^^^^

error: aborting due to previous error

this is with

hyper = "0.12"

and

[macro_use]

extern crate hyper;

maybe i'm missing something?

To learn more, run the command again with --verbose.

On Sun, Sep 16, 2018 at 1:04 PM Scott Schroeder notifications@github.com
wrote:

There may be a better way I don't know about, but the header!
https://docs.rs/hyper/0.11.1/hyper/header/index.html#defining-custom-headers
macro might do what you need without too much boilerplate.

[macro_use]extern crate hyper;extern crate reqwest;

header! { (SpecialHeader, "special-header") => [String] }
fn main() {
reqwest::Client::new()
.get("http://localhost:8001/")
.header(SpecialHeader("foo".to_owned()))
.send().unwrap();
}

127.0.0.1 - - [16/Sep/2018 12:58:26] "GET / HTTP/1.1" 200 -
User-Agent: reqwest/0.8.8
Accept: /
special-header: foo
Accept-Encoding: gzip

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/seanmonstar/reqwest/issues/346#issuecomment-421823751,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAR8mu52tzLYX3DWO1tg8TOEYXMeQbw3ks5ubq7jgaJpZM4Wqlp0
.

I was using hyper = "0.11.22", which is what the 0.8.x release of reqwest uses.

The good news, is that in the master branch for reqwest, you can do exactly what you suggested initially...

// reqwest = { git = "https://github.com/seanmonstar/reqwest" }
extern crate reqwest;

fn main() {
    reqwest::Client::new()
        .get("http://localhost:8001/")
        .header("special-header", "foo")
        .send().unwrap();
}

Oh wow! Awesome :) thank you

On Sun, Sep 16, 2018 at 1:53 PM Scott Schroeder notifications@github.com
wrote:

I was using hyper = "0.11.22", which is what the 0.8.x release of reqwest
uses.

The good news, is that in the master branch for reqwest, you can do
exactly what you suggested initially...

// reqwest = { git = "https://github.com/seanmonstar/reqwest" }extern crate reqwest;
fn main() {
reqwest::Client::new()
.get("http://localhost:8001/")
.header("special-header", "foo")
.send().unwrap();
}

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/seanmonstar/reqwest/issues/346#issuecomment-421831571,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAR8ms6a6Mpbrji4-IOIOBjCed3Ccp_Yks5ubro7gaJpZM4Wqlp0
.

I wish it would be in the docs...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

martinlindhe picture martinlindhe  Â·  3Comments

apiraino picture apiraino  Â·  3Comments

encombhat picture encombhat  Â·  6Comments

edouardmenayde picture edouardmenayde  Â·  4Comments

kpcyrd picture kpcyrd  Â·  4Comments