【Axum】发送 JSON 请求体
环境
- Time 2022-01-16
- Rust 1.58.0
- Axum 0.4.4
概念
参考:https://docs.rs/axum/latest/axum/index.html
示例
main.rs
use axum::{routing::post, Json, Router};
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
#[tokio::main]
async fn main() {
let app = Router::new().route("/", post(home));
let addr = SocketAddr::from(([127, 0, 0, 1], 4444));
println!("listening on {addr}");
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
#[derive(Serialize, Deserialize, Debug)]
struct User {
id: usize,
name: String,
}
async fn home(Json(user): Json) -> Json {
println!("{:#?}", user);
Json(User { id: 44, ..user })
}
总结
使用 JSON 请求体发送 POST 请求,返回 JSON。