Volga
Home
API Docs
GitHub
  • English
  • Русский
Home
API Docs
GitHub
  • English
  • Русский
  • Home
  • Getting Started

    • Quick Start
    • Route Parameters
    • Query Parameters
    • Route Groups
  • Requests & Responses

    • Headers
    • Handling JSON
    • Handling Form Data
    • Working with Files
    • Cookies
  • Middleware & Infrastructure

    • Basic Middleware
    • Custom Middleware
    • Response Compression
    • Request Decompression
    • CORS (Cross-Origin Resource Sharing)
    • Static Files
    • Rate Limiting
  • Security & Access

    • Authentication and Authorization
  • Reliability & Observability

    • Global Error Handling
    • Tracing & Logging
    • Request cancellation
  • Protocols & Realtime

    • HTTP/1 and HTTP/2
    • HTTPS
    • WebSockets
    • Server-Sent Events (SSE)
  • Advanced Patterns

    • Dependency Injection
    • Custom Handling of HEAD, OPTIONS, and TRACE Methods

Route Parameters

Volga offers robust routing configurations allowing you to harness dynamic routes using parameters. By utilizing the function arguments that implement the FromStr trait, you can pass them directly to your request handler.

Example: Single Route Parameter

Here's how to set up a simple dynamic route that greets a user by name:

use volga::{App, ok};

#[tokio::main]
async fn main() -> std::io::Result<()> {
    let mut app = App::new();

    app.map_get("/hello/{name}", |name: String| async move {
        ok!("Hello {}!", name)
    });

    app.run().await
}

Testing the Route

In the curly brackets, we described the GET route with a name parameter, so if we run requests over the HTTP API, it will call the desired handler and pass an appropriate name value as a function argument.

Using the curl command, you can test the above configuration:

> curl "http://localhost:7878/hello/world"
Hello world!

> curl "http://localhost:7878/hello/earth"
Hello earth!

> curl "http://localhost:7878/hello/sun"
Hello sun!

Example: Multiple Route Parameters

You can also configure multiple parameters in a route. Here’s an example:

use volga::{App, ok};

#[tokio::main]
async fn main() -> std::io::Result<()> {
    let mut app = App::new();

    app.map_get("/hello/{descr}/{name}", |descr: String, name: String| async move {
        ok!("Hello {} {}!", descr, name)
    });

    app.run().await
}

When you run the following curl command, it will return:

> curl "http://localhost:7878/hello/beautiful/world"
Hello beautiful world!

Warning

It is important to strictly keep the order of the arguments for the handler function as described in the route. So for the hello/{descr}/{name} it is supposed to be |descr: String, name: String|.

Using NamedPath<T>

Alternatively, use the NamedPath<T> to wrap the route parameters into a dedicated struct. Where T should be either deserializable struct or HashMap. Make sure that you also have serde installed:

use volga::{App, NamedPath, ok};
use serde::Deserialize;
 
#[derive(Deserialize)]
struct User {
    name: String,
    age: u32
}

#[tokio::main]
async fn main() -> std::io::Result<()> {
    let mut app = App::new();

    // GET /hello/John/35
    app.map_get("/hello/{name}/{age}", |user: NamedPath<User>| async move {
        // Here you can directly access the user struct fields
        ok!("Hello {}! Your age is: {}!", user.name, user.age)
    });

    app.run().await
}

Using these examples, you can add dynamic routing to your Volga-based web server, enhancing the flexibility and functionality of your applications.

Check out the full example here

Last Updated: 1/18/26, 6:33 PM
Prev
Quick Start
Next
Query Parameters