Reqwest get function

The get function declared in reqwest crate as follows.

pub async fn get<T: IntoUrl>(url: T) -> Result

In my program , I have like this

let resp1 = reqwest::blocking::get("http://debianmirror.nkn.in/debian").expect("Error in opening the url");

I'm passing &str . How do &str is of type T which implements the trait IntoUrl ?.

I see the trait IntoUrl is defined in reqwest.

I suppose Alice will help immediately!!!

Thanks,
S.Gopinath

Maybe if you use code blocks :slight_smile:

```
// your code here
```
1 Like

The get function declared in reqwest crate as follows.

pub async fn get<T: IntoUrl>(url: T) -> Result

In my program , I have like this

let resp1 = reqwest::blocking::get("http://debianmirror.nkn.in/debian").expect("Error in opening the url");

I'm passing &str . How do &str is of type T which implements the trait IntoUrl ?.

I see the trait IntoUrl is defined in reqwest.

I suppose Alice will help immediately!!!

Thanks,
S.Gopinath

This is how it looks with proper formatting:

The get function declared in reqwest crate as follows.

pub async fn get<T: IntoUrl>(url: T) -> Result

In my program, I have like this

let resp1 = reqwest::blocking::get("http://debianmirror.nkn.in/debian").expect("Error in opening the url");

I'm passing &str. How do &str is of type T which implements the trait IntoUrl?.

I see the trait IntoUrl is defined in reqwest.

which I did with the following markup:

The get function declared in reqwest crate as follows.

```
pub async fn get<T: IntoUrl>(url: T) -> Result
```

In my program, I have like this

```
let resp1 = reqwest::blocking::get("http://debianmirror.nkn.in/debian").expect("Error in opening the url");
```

I'm passing `&str`. How do `&str` is of type `T` which implements the trait `IntoUrl`?.

I see the trait `IntoUrl` is defined in reqwest.

Please take care to use proper formatting in the future. Notice that I also have single backticks around types in the post, and I avoid having empty lines in the code block.

As for your question, you can look at the IntoUrl trait's documentation, but unfortunately reqwest uses a private type to seal the trait, which also hides the list of possible url types from the documentation. You have to actually view the source code to find the list. They give this reason for doing it like this:

This trait is "sealed", such that only types within reqwest can implement it. The reason is that it will eventually be deprecated and removed, when std::convert::TryFrom is stabilized.

If you take a look at the source code, you will find the following impls:

impl PolyfillTryInto for Url { ... }
impl<'a> PolyfillTryInto for &'a str { ... }
impl<'a> PolyfillTryInto for &'a String { ... }

So there are three types you can use as an url:

  1. &str
  2. &String
  3. Url

Any other type would have to be converted into one of the three types above first.

1 Like

Super!!!

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