How to convert a http::Request to a reqwest::Request?

Hello!
How to convert a http::Request to reqwest::Request?

I am trying use the method try_from:
https://docs.rs/reqwest/0.11.4/reqwest/struct.Request.html#method.try_from-1
but not working correct.

Code:

 //...
 // Vec<u8> to String
 let text = String::from_utf8(buf).unwrap();
 println!("HTTP text: {}", text);

 use http::Request;
 let request = Request::new(text);

 use reqwest::Request;
 match reqwest::Request::try_from(request)
 {
     Ok(req) => { println!("HTTP req: {:?}", req); },
     Err(e) => { println!("HTTP e: {:?}", e); }
 };

Output:

HTTP text: GET http://wp.pl/ HTTP/1.1
Host: wp.pl
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: pl,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Cookie: statid=a12569ee096991ddcfe0bb719c1b972a:88da77:1557495429:v3; gusid=2e46d0558982fd569c0e7ec87a8e758c; OAX=UEHVIV0nh2MAA8or; uids=eyJ0ZW1wVUlEcyI6eyJhZG54cyI6eyJ1aWQiOiIwIiwiZXhwaXJlcyI6IjIwMjEtMDYtMDhUMjE6MjU6MDcuMzQ3OTY4NTc3KzAyOjAwIn0sIml4Ijp7InVpZCI6IjAiLCJleHBpcmVzIjoiMjAyMS0wNi0wOFQyMToyNTowNi44NzA4ODQxNjErMDI6MDAifSwicnViaWNvbiI6eyJ1aWQiOiJLUDRGSEI2TS1VLUZQNVIiLCJleHBpcmVzIjoiMjAyMS0wNi0wOFQyMToyNTowNi45NDc4ODUxODMrMDI6MDAifSwic21hcnRhZHNlcnZlciI6eyJ1aWQiOiI0NzA4ODU0MTY5MDE0NDU1NzkzIiwiZXhwaXJlcyI6IjIwMjEtMDYtMDhUMjE6MjU6MDcuOTg3OTcxODM0KzAyOjAwIn19LCJiZGF5IjoiMjAyMS0wNS0yNVQyMToyNTowNS45NDAwNzk1NzkrMDI6MDAifQ==
Upgrade-Insecure-Requests: 1


HTTP e: reqwest::Error { kind: Builder, source: RelativeUrlWithoutBase }

Raw HTTP request:

GET http://wp.pl/ HTTP/1.1\r\nHost: wp.pl\r\nUser-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\nAccept-Language: pl,en-US;q=0.7,en;q=0.3\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\nCookie: statid=a12569ee096991ddcfe0bb719c1b972a:88da77:1557495429:v3; gusid=2e46d0558982fd569c0e7ec87a8e758c; OAX=UEHVIV0nh2MAA8or; uids=eyJ0ZW1wVUlEcyI6eyJhZG54cyI6eyJ1aWQiOiIwIiwiZXhwaXJlcyI6IjIwMjEtMDYtMDhUMjE6MjU6MDcuMzQ3OTY4NTc3KzAyOjAwIn0sIml4Ijp7InVpZCI6IjAiLCJleHBpcmVzIjoiMjAyMS0wNi0wOFQyMToyNTowNi44NzA4ODQxNjErMDI6MDAifSwicnViaWNvbiI6eyJ1aWQiOiJLUDRGSEI2TS1VLUZQNVIiLCJleHBpcmVzIjoiMjAyMS0wNi0wOFQyMToyNTowNi45NDc4ODUxODMrMDI6MDAifSwic21hcnRhZHNlcnZlciI6eyJ1aWQiOiI0NzA4ODU0MTY5MDE0NDU1NzkzIiwiZXhwaXJlcyI6IjIwMjEtMDYtMDhUMjE6MjU6MDcuOTg3OTcxODM0KzAyOjAwIn19LCJiZGF5IjoiMjAyMS0wNS0yNVQyMToyNTowNS45NDAwNzk1NzkrMDI6MDAifQ==\r\nUpgrade-Insecure-Requests: 1\r\n\r\n

http::Request::new takes request body, not the full request with headers. Not sure if it's possible to build it this way - quick search through docs didn't reveal anything.

Thanks for your reply.

I need convert a Vec<u8> to reqwest::Request.
but I can't use the reqwest to parse a http query.

A Vec of what?

@Cerberuser Vec<u8>

I mean, what does this Vec contain, semantically? If it's the raw bytes of HTML request (the buf in the original code), then why do you need reqwest at all?

Yes. It's raw bytes of http request.
I am writing a simple proxy server/http packet inspector. I need a request to validate and debug.

I just trying to parse an http request using the hyper library.
And I have a similar problem : hyper::Error(User(AbsoluteUriRequired))
Is the query corrupted, incorrect?

Web browser: Mozilla Firefox

Looks like so IMO:

GET http://wp.pl/ HTTP/1.1
Host: wp.pl

this should rather be

GET / HTTP/1.1
Host: wp.pl

(the path part should start from /).

Also, as @Cerber-Ursi suggests, neither the http library nor the reqwest library don't provide API for HTTP parsing, which is what you want. The http::Request::new method was certainly not what you wanted (interesting where the error came from then..., as it suggests some parsing was done. Perhaps http::Request has some defaults for headers, which are invalid in eyes of reqwest::Request?)

Anyway, perhaps httparse is what you want? Seems to be the parsing crate used by hyper (and transitively, by reqwest).

httparse is working well. But I need send the request to server (forward).

I need a solution that make:

  1. Parses a http request.
  2. Sends the http request to the next http server (target).

Use httparse to get all components of request.

Use reqwest::Reqwest methods to build the request from components, provided by the first step.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.