-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_params.rs
58 lines (49 loc) · 1.69 KB
/
basic_params.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use axum::{http::StatusCode, response::IntoResponse, routing::post, Json, Router};
use axum_params::Params;
use serde::{Deserialize, Serialize};
// Simple parameters with path, query, and optional fields
#[derive(Debug, Deserialize, Serialize)]
struct TestParams {
id: i32, // Path parameter (/users/:id)
name: String, // From JSON or form
#[serde(default)]
extra: Option<String>, // Optional query parameter
}
#[axum::debug_handler]
async fn test_params_handler(Params(test, _): Params<TestParams>) -> impl IntoResponse {
// Access parameters naturally
println!("ID: {}, Name: {}", test.id, test.name);
(StatusCode::OK, Json(test))
}
#[tokio::main]
async fn main() {
// Build our application with a route
let app = Router::new().route("/users/:id", post(test_params_handler));
// Run it
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
println!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap();
}
/*
Test with curl:
# Combined path, query, and JSON parameters
curl -X POST "http://localhost:3000/users/123?extra=additional" \
-H "Content-Type: application/json" \
-d '{"name": "John Doe"}'
Expected response:
{
"id": 123,
"name": "John Doe",
"extra": "additional"
}
# Form data instead of JSON
curl -X POST "http://localhost:3000/users/123?extra=additional" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "name=John%20Doe"
# Test using this source file as form data
curl -X POST "http://localhost:3000/users/123?extra=additional" \
-H "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "name@examples/basic_params.rs"
*/