Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to send a http request using only the http crate?

So I looked up the http crate docs and used Request::builder() to make a Request. However, as stupid as it sounds, I did not manage to find out how to send this request. Like what do I need to do? :p

There is a example code, but it cuts out the important part, the send() function:

use http::{Request, Response};

let mut request = Request::builder()
    .uri("https://www.rust-lang.org/")
    .header("User-Agent", "my-awesome-agent/1.0");

if needs_awesome_header() {
    request = request.header("Awesome", "yes");
}

let response = send(request.body(()).unwrap());

fn send(req: Request<()>) -> Response<()> {
    // ...
}

I already managed to send requests via hyper, following the starter guide, but the http crate seems easier to understand to me…at least as long as I don’t have to send my Request :p

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Currently I have:

use http::{Request, Response, StatusCode};

fn main() {
    let request = Request::builder()
        .method("GET")
        .uri("https://api.spotify.com/v1/search?q=system+overload")
        .header("artist", "smash stereo")
        .body(())
        .unwrap();
    println!("{:?}", request);

}

which outputs:

Request { method: GET, uri: https://api.spotify.com/v1/search?q=system+overload, version: HTTP/1.1, headers: {"artist": "smash stereo"}, body: () }

>Solution :

You cannot. The crate documentation itself states:

You will notably not find an implementation of sending requests or spinning up a server in this crate. It’s intended that this crate is the “standard library” for HTTP clients and servers without dictating any particular implementation.

You can use http types with other libraries that do support sending requests like:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading