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

[Question] : How to add a default header map to HttpClientBuilder? #180

Closed
ansrivas opened this issue Apr 25, 2020 · 2 comments
Closed

[Question] : How to add a default header map to HttpClientBuilder? #180

ansrivas opened this issue Apr 25, 2020 · 2 comments
Labels
feature A new feature!

Comments

@ansrivas
Copy link
Contributor

Hi, thank you for your work in creating this project.

I am trying to inject a default HeaderMap in client builder. I saw that there is SetOpt implementation as well but no method in Configurable to append the headers.

How can I achieve something like this?

let mut headers = HeaderMap::new();
headers.insert(
  "X-header-something",
  HeaderValue::from_str("my-value").unwrap(),
);

let c = isahc::HttpClientBuilder::new()
    .set_default_headers(headers) // <---- Something like this
    .build()?;  
@sagebind sagebind added the feature A new feature! label Apr 25, 2020
@sagebind
Copy link
Owner

Great question! This isn't currently possible, but its a great idea for a feature! I imagine it would look something like this:

impl HttpClientBuilder {
    // etc

    pub fn default_header(self, key: impl TryInto<HeaderName>, value: impl TryInto<HeaderValue>) -> Self {}

    pub fn default_headers_mut(&mut self) -> &mut HeaderMap<HeaderValue> {}
}

I saw that there is SetOpt implementation as well

That's just used as an internal helper for setting the headers provided in a Request given. Implementing default headers for a client would leverage the same code path.

@ansrivas ansrivas mentioned this issue Apr 26, 2020
sagebind pushed a commit that referenced this issue May 3, 2020
* Might fix #184

Signed-off-by: Ankur Srivastava <best.ankur@gmail.com>

* handle review

Signed-off-by: Ankur Srivastava <best.ankur@gmail.com>

* more test cases

Signed-off-by: Ankur Srivastava <best.ankur@gmail.com>

* trying to match with Any in tests

Signed-off-by: Ankur Srivastava <best.ankur@gmail.com>

* use keys to iterate over HeaderMap

Signed-off-by: Ankur Srivastava <best.ankur@gmail.com>

* removed DefaultHeaderMap implementation, not using .set_opt anymore

Signed-off-by: Ankur Srivastava <best.ankur@gmail.com>

* format

Signed-off-by: Ankur Srivastava <best.ankur@gmail.com>

* review comments:
- Keep the error as enum http::Error
- Commented out the failing tests until it gets fixed upstream in mockito

Signed-off-by: Ankur Srivastava <best.ankur@gmail.com>
@sagebind
Copy link
Owner

This feature is now available in 0.9.2! 🎉 Thanks to @ansrivas for implementing this!

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

No branches or pull requests

2 participants