Reqwest RequestBuilder String-Slice to HeaderValue

This is about struct reqwest::blocking::RequestBuider.

There is a method called

pub fn header <K, V>(self, key: K, value: V) -> [RequestBuilder]

The example given is as follows

use reqwest::header::USER_AGENT;

let client = reqwest::blocking::Client::new();
let res = client.get("https://www.rust-lang.org")
    .header(USER_AGENT, "foo")
    .send()?;

I got it that USER_AGENT is defined as a const of HeaderName.

I would like to know how the string slice "foo" is translated to HeaderValue.

Thanks,
S.Gopinath

Let's take a look at the full signature:

pub fn header<K, V>(self, key: K, value: V) -> RequestBuilder where
    HeaderName: TryFrom<K>,
    HeaderValue: TryFrom<V>,
    <HeaderName as TryFrom<K>>::Error: Into<Error>,
    <HeaderValue as TryFrom<V>>::Error: Into<Error>, 

So to call the function, the HeaderValue type must implement the TryFrom<V> trait. If you check out the Trait Implementations section on HeaderValue, you will see that an impl for V = &str indeed does exist.

impl<'a> TryFrom<&'a str> for HeaderValue

So the string slice "foo" is converted to a HeaderValue through that TryFrom impl.

Hi,

One clarification is required for me. Please help

I can get it the trait bounds in the function signature as given as example in the Rust book

fn simple_function<T,U> ( t: T, u: U) ->f64
    where  T :  Summary,
           U :  Display
{

       //Function body
}

I cant get the syntax as here

pub fn header<K, V>(self, key: K, value: V) -> RequestBuilder where
    HeaderName: TryFrom<K>,
    HeaderValue: TryFrom<V>,
    <HeaderName as TryFrom<K>>::Error: Into<Error>,
    <HeaderValue as TryFrom<V>>::Error: Into<Error>, 

Can you explain it.

It might seem unusual compared to T: Display, but it is the same syntax. You have some type and some trait, and the type must implement the trait.

SomeType: SomeTrait

It's just that normally we use a generic parameter in place of SomeType, but there's no requirement that we do this.

It looks for me more than the "pattern" substitution.

pub fn header<K, V>(self, key: K, value: V) -> RequestBuilder where
    HeaderName: TryFrom<K>,
    HeaderValue: TryFrom<V>,
    <HeaderName as TryFrom<K>>::Error: Into<Error>,
    <HeaderValue as TryFrom<V>>::Error: Into<Error>, 
```
HeaderName is a stuct and here it means it implements TryFrom<K>.

But match of pattern is not clear to me.

It is not a pattern. To give an example, this is the impl block of TryFrom<&str> for HeaderValue.

impl<'a> TryFrom<&'a str> for HeaderValue {
    type Error = InvalidHeaderValue;
    fn try_from(t: &'a str) -> Result<Self, Self::Error> { ... }
}

The <HeaderValue as TryFrom<V>>::Error syntax would in this case evaluate to InvalidHeaderValue from the impl block above. So the last bound says that if conversion into header value fails, the error must be convertible into hyper's Error type.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.