httprustrequestgetsend

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

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: