【发布时间】:2021-11-24 07:05:13
【问题描述】:
我在玩actix-web 时遇到了以下问题。 我有 2 个函数,一个函数接受后字符串请求并返回结构的 json,另一个接受结构的 json 并返回字符串。但是,采用 json 结构会出现错误 400。
结构代码
#[derive(Deserialize, Serialize)]
pub struct MyObj {
pub name: String,
}
功能
#[post("/simple")]
pub async fn simple(obj: String) -> impl Responder {
MyObj {
name: obj,
}
}
#[post("/simplerecv")]
pub async fn simplerrecv(_obj: web::Json<MyObj>) -> String {
println!("Inside simplerecv");
"Hello".to_owned()
}
用于测试的 Python 代码
def simple():
# testing simple
data = "hello44444"
r1 = requests.post("http://127.0.0.1:8080/simple", data=json.dumps(data))
print(r1.status_code, type(r1.text), r1.text)
res = json.loads(r1.text)
r2 = requests.post("http://127.0.0.1:8080/simplerecv",
data=json.dumps(json.loads(r1.text)))
print(r2.status_code)
Python 代码的输出
200
400
【问题讨论】: