Rust: Extracting Request Data with Actix-web

John Philip
Rustaceans
Published in
2 min readFeb 22, 2024

--

Actix web Log

Actix-web is a powerful Rust web framework known for its speed, reliability, and ease of use. One of its key features is its ability to efficiently handle and process incoming HTTP requests.

In this article, we’ll dive into how we can leverage Actix-web to extract various components of an HTTP request, including the request body, query parameters, path, and HTTP methods.

Extracting Request Parameters

Let’s start by examining the code snippet below:

use actix_web::{get, HttpRequest, HttpResponse};
use bytes::Bytes;
use std::collections::HashMap;

#[get("/get")]
pub async fn get_responder(req: HttpRequest, body: Bytes) -> HttpResponse {
// Extract path from request
let path = req.uri().path().to_string();

// Extract headers from request
let headers: HashMap<String, String> = req
.headers()
.iter()
.map(|(name, value)| (name.as_str().to_string(), value.to_str().unwrap_or("").to_string()))
.collect();

// Extract query parameters from request
let query_params: HashMap<String, String> = req
.uri()
.query()
.map(|query| {
query
.split('&')
.filter_map(|param| {
let mut parts = param.split('=');
let key =…

--

--