reqwest: Can't use relative URLs for making requests
When trying to pass a relative URL to any of the functions like .get, it errors with RelativeUrlWithoutBase. From my understanding, this is triggered by the url crate because Url::parse only accepts absolute URLs.
My expectation would have been that I can set the Host header in the default header map on the client instance and later on use relative URLs to actually make requests.
Is this something that is planned to be supported?
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 39
- Comments: 18 (6 by maintainers)
Another independent use case for this is for the WASM browser target. It would be convenient for clients to be able to specify relative URLs from the host they are on
FWIW I was just searching for a similar feature for integration tests, thought being that instead of creating a client in each test and passing an address, I could just generate a client which points at the locally running server and pass that to each test. Not a big deal, but would reduce a couple of lines of boilerplate per test.
Instead of this:
I could have this:
Although in reality it’d probably be a reusable rtest fixture so I can make my Rust server tests as easy to write as FastAPI tests. And that brings me to my answer to this:
Coming from the Python world, httpx has this feature. It’s the most popular of the “modern” (i.e., async capable) request libraries.
In the Rust world, surf supports this syntax as well. I don’t think any of the others I’ve tried do.
This would be cool to have.
on wasm, for yew or other, you can get the base url (https://youraddress.com) with
let baseurl = web_sys::window().unwrap().origin();reqwasmalso supports relative urls.Here is a way around using relative paths
surfallows one to specify abase_urlin the client config builder and then allows relative urls in requests made using that client.Hello,
python httpx supports
base_urlon the Client builderhttps://www.python-httpx.org/advanced/#other-client-only-configuration-options
Axum’s
TestClienthelper (for integration testing) might be a useful example for anyone looking for a simple wrapper around some ofClient’s methods to allow for relative URLs, but this is opinionated towards Axum integration testing:https://github.com/tokio-rs/axum/blob/main/axum/src/test_helpers/test_client.rs
And if you happen to be specifically looking for a solution for Axum integration testing, it’s also available in a crate: https://crates.io/crates/axum-test-helper
request, the major node (javascript) HTTP client library, does.