To create a http server with a head route in Rust, we will use the hyper
crate which is a popular library for building HTTP servers and clients in Rust.
Here is a basic example of how to create a HTTP server with a HEAD
route in Rust:
main.rs932 chars28 lines
In this example, we defined an async
function called head_handler
that takes a Request
instance as input and returns a Result<Response<Body>, hyper::Error>
instance. We use the Response::new(Body::empty())
method to create an empty body for the HTTP HEAD
request. We then insert the header Content-Length: 0
to indicate the size of the empty body.
We then create a new service function using the make_service_fn
function and pass in our head_handler
function. This function will handle incoming connections on port 3000
and respond with a header indicating the size of an empty response body for HTTP HEAD
requests.
Finally, we use the Server::bind()
method to bind the server to the specified addr
(127.0.0.1:3000) and start the server using the await
method.
gistlibby LogSnag