我在玩actix,遇到了下面的问题。我有两个函数,一个函数接受post字符串请求并返回一个struct的json,另一个函数接受struct的json并返回string。但是,采用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 {“名称”:“hello44444”}
400
发布于 2021-10-03 16:01:08
有些web服务器不接受没有Content-Type: application/json的JSON,actix也是如此。
r2 = requests.post("http://127.0.0.1:8080/simplerecv",
headers={'Content-Type': 'application/json;charset=utf-8'},
data=json.dumps(json.loads(r1.text)))https://stackoverflow.com/questions/69426099
复制相似问题