To create an HTTP server in Rust and read the request body as HTML, you can use the hyper
and select
crates. Here's an example code that demonstrates the process:
main.rs1757 chars47 lines
This code defines an asynchronous function handle_request
that receives a hyper Request
and returns a Response
. It checks if the request method is POST
and the Content-Type
header starts with "text/html"
. If so, it reads the request body, parses the HTML document using the select
crate, and returns the <title>
element as the response body. Otherwise, it returns a 404 error.
The main function binds the server to port 8080, creates a service for each incoming connection using the handle_request
function, and starts the server. You can run this code with cargo run
and send a POST request with HTML content to http://localhost:8080/
to test it.
gistlibby LogSnag