0

Rust – Capture Path Parameter and JSON Payload in Actix-web

This post demonstrates how to capture path parameters and JSON  payload in Actix-web .

Sample REST API

Assuming we have the following URL for our REST API.

URL

JSON Payload

Sample Server Codes

Cargo.toml

structs for Request and Response

The JSON  payload is represented by the JSONPayload  struct . The Respose struct  is for our empty JSON  response.

Request Handler

To capture both path parameters and the JSON payload, we use web::Path  and web::Json  types for the function parameters.

In the handler codes, we can access the path parameter and JSON  payload via the path_param  and payload  variables.

Main function

Test with Postman

Headers

Body

Click Send button to proceed.

HTTP Response

Console Output

Tested with Rust 1.37.0.

Karl San Gabriel

Karl San Gabriel

Professional Software Developer