Post requestを受け取るにはactix-webを使う。
まず、受信のテストから。
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder}; #[post("/handshake")] async fn handshake(req_body: String)-> impl Responder { println!("{}", req_body); HttpResponse::Ok().body(req_body) } #[actix_web::main] async fn main() -> std::io::Result<()> { HttpServer::new(||{ App::new() .service(handshake) }) .bind(("127.0.0.1", 8080))? .run() .await }
postする
$ curl -X POST -d “{\”time\”:\”2024-12-25 22:53:36.824066840 UTC\”,\”sender\”:\”4bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991123\”,\”receiver\”:\”4bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991124\”,\”amount\”:10,\”signature\”:\”8000E340A55A517D0F27F3A63FBE39ED576BA491DFAC89B44654AB147EC66B206B054BAAF53E318EB2721DC892B4736630F400547989AE9F7C069034ECB4DF98\”}”
サーバ側
Finished `dev` profile [unoptimized + debuginfo] target(s) in 3.70s
Running `target/debug/hoge`
{“time”:”2024-12-25 22:53:36.824066840 UTC”,”sender”:”4bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991123″,”receiver”:”4bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991124″,”amount”:10,”signature”:”8000E340A55A517D0F27F3A63FBE39ED576BA491DFAC89B44654AB147EC66B206B054BAAF53E318EB2721DC892B4736630F400547989AE9F7C069034ECB4DF98″}
受信できてます。
ここで受け取ったデータからverifyすればOKですね。
use serde::{Serialize, Deserialize}; #[derive(Debug, Serialize, Deserialize)] struct SignedTransaction { time: String, sender: String, receiver: String, amount: i32, signature: String, } #[post("/handshake")] async fn handshake(req_body: String)-> impl Responder { let req : SignedTransaction = serde_json::from_str(&req_body).unwrap();; println!("{}", req.signature); HttpResponse::Ok().body(req_body) }
Postされたデータはjsonにして、SignedTransactionの構造体で受け取ると、req.signatureというように、値を取り出すことができる。