Rust, Software Development

Working with Actix-Web HTTP Requests

When working with Actix-Web, we sometimes need to access incoming HTTP requests to inspect for some values. In that case, we need to use the HttpRequest struct either in request handlers and middlewares.


We will use the following dependencies for the code examples.

HTTP Requests in Middlewares

In middlewares, HTTP requests are accessible via the request-wrapper ServiceRequest struct. It has public functions with the same signature as those of the HttpRequest’s.

See Actix-Web Basic And Bearer Authentication Examples for sample middlewares.

HTTP Requests in Request Handlers

With request handlers, we only need to specify the HttpRequest struct as one of the function arguments. Consider the following function that does not include HttpRequest in its argument list.

We can change the function signature to accept an instance of HttpRequest struct.

Working With HTTP Request Headers

To display HTTP request headers, we can loop through an instance of HeaderMap.

The output is as follows.

Get the HTTP Request URI

To display the HTTP request URI in Actix-Web, we can use the following codes.

The output is as follows.

Get the HTTP Request Method

To display the HTTP method, we can use the following codes.

The output is as follows.

Get HTTP Version

To display the HTTP version, we use the following codes.

The generated output is as follows.

Get HTTP Peer Address

The peer address means socket address. It specifies the IP (Internet Protocol) version, the remote IP address, and the server port the request went through.

The output is as follows.

Get Host Name in Actix-Web

Aside from the HTTP headers, the hostname is also available via Actix-Web’s AppConfig.

Working with AppConfig to Check For Secure Connections

We can also check if Actix-Web using HTTPS, and the requests come in from a secure connection.

To test with a secure Actix-Web, please see Secure Actix-Web Application With TLS.

Working with Actix-Web HTTP requests via the HttpRequest may not be for everyone. But when we write applications that rely on HTTP-related information, the HttpRequest struct is the only way to access that information.


Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.

You Might Also Like