rust web服务器接收发送json参数

rust web服务器接收发送json参数

当时用rust写web服务器数据传输这块坑点不少,特此记录给有需要的同学。
原文链接:https://blog.csdn.net/weixin_44259356/article/details/104587536

配置文件

serde_json = "1.0"
actix-web = "2.0"
actix-rt = "1.0"

导包

use actix_web::{web, App, HttpRequest, HttpServer, Responder};
use serde::{Deserialize, Serialize};
use serde_json::Value;

接口函数定义

async fn xxx(_: HttpRequest, _info: web::Json<Value>) -> impl Responder {
}

json参数获取

let user_id = _info.get("data").unwrap().get("xxx").unwrap();

json返回数据定义

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
struct result {
    code: i32,
    message: String,
}

json数据返回

serde_json::to_string(&result {
    code: code,
    message: msg,
})

postman json请求格式

在这里插入图片描述

postman json数据

在这里插入图片描述

发布了46 篇原创文章 · 获赞 6 · 访问量 9390

猜你喜欢

转载自blog.csdn.net/weixin_44259356/article/details/104587536