HTTP Crate with URL Support & a Simple HTTP Client
pyfisch.org/blog/http-with-url/Posted Jun 1, 2018, 4:36 PM UTC
Posted Jun 1, 2018, 4:36 PM UTC
I used to use http crate for mio_httpc but eventually abandoned it. In my opinion http is only useful for servers.
First of all it results in too many types. When creating an HTTP request you also need some http call settings like timeout, max response size, max redirects, etc. You are now using at least two builder types. This is ugly and unnecessary.
In any non-trivial app where you need a HTTP client, you will also be constructing your URLs. Correct URLs require percent encoding. Neither http nor url crates help the user with this problem. The only proper solution is for the http client to allow and encourage URL construction piece by piece.
Concatenating strings should be avoided and HTTP clients should help the user to avoid it.
What resulted is everything being done using a CallBuilder.
CallBuilder::get()
.https()
.host("www.example.com")
.auth("user name", "my password")
.port(12345)
.path_segm("a/")
.path_segm("b")
.query("spaced key", "123")
.query("key", "<>")
.get_url();
I do think that the http crate could benefit from a uri::Builder! Would you be interested in helping us flesh out a design and implementation, to help the crate be more useful in client situations?
I also don't think that adding your own builders that encapsulate http's builders is bad.
I would be happy to help with the design.
Would you want to open an issue on the http repo, outlining goals or a proposed API? We can discuss ergonomics and validation there!
The linked issue (https://github.com/hyperium/http/issues/73) explains why the
httpcrate does not use theurlcrate.Also from the linked issue (by me):
This is open source. The issue already indicated that a conversion would be fine. The time spent authoring this blog post could have been spent authoring a PR that provided the conversions and the feature released.
Instead, we are going to have an argument on the internet...
My intend was it to completely replace http:Uri and just use url::Url. From earlier comments on both hyper and http I got the impression that the core team had already decided not use (or integrate it otherwise) url::Url and did want to discuss this further.
Comments like this by @seanmonstar about Url:
Anyways I've created a PR for conversions.
Sorry, I didn't mean to imply go away or I don't want to talk about! With that comment, I originally meant that from the context of the
httpcrate, it seemed best to me not includeurl. Reading it again now though, I see how it can sound dismissive.I do still feel like conversions aren't that bad. I say this as the maintainer of
reqwest, where I convert between the two types internally when needed.And I still suspect the downsides outweigh the benefits.
But I don't mind hearing why I could have wrong feelings :D