Axios POST请求错误,来自React的422(无法处理的实体)

编程入门 行业动态 更新时间:2024-10-28 16:28:18
本文介绍了Axios POST请求错误,来自React的422(无法处理的实体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

来自FastAPI&;SqlalChemy

@app.post("/users") def create_users(email: str, pwd: str, first_name: str, last_name: str, phone_number: str, city: str): user = UserTable() user.email = email user.pwd = pwd user.first_name = first_name user.last_name = last_name user.phone_number = phone_number user.city = city session.add(user) sessionmit() return f"{email} created..."

反应axios.POST请求

const addUserHandler = () => { console.log(email, pwd, first_name, last_name, phone_number, city); axios .post('localhost:8000/users', { email: email, pwd: pwd, first_name: first_name, last_name: last_name, phone_number: phone_number, city: city, }) .then((res) => console.log(res.data)) .catch((error) => { console.log(error.response.data); }); console.log(city); };

下面是错误代码

xhr.js:177 POST localhost:8000/users 422 (Unprocessable Entity) App.js:37 {detail: Array(6)} detail: Array(6) 0: {loc: Array(2), msg: "field required", type: "value_error.missing"} 1: {loc: Array(2), msg: "field required", type: "value_error.missing"} 2: {loc: Array(2), msg: "field required", type: "value_error.missing"} 3: {loc: Array(2), msg: "field required", type: "value_error.missing"} 4: {loc: Array(2), msg: "field required", type: "value_error.missing"} 5: {loc: Array(2), msg: "field required", type: "value_error.missing"} length: 6 __proto__: Array(0) __proto__: Object

代码来自Reaction&;AXIOS,我收到了422错误,无法开机自检。我确实检查了变量(UseState)有一个Each字符串。但错误仍然显示";字段必填";和";Value_error.Missing&Quot;。我如何修复此问题?

谢谢您的回复!

推荐答案

问题在于,您需要的是"query params"位于url(Fast Api)中,而不是json正文。

所以,

@app.post("/users") def create_users(email: str, pwd: str, first_name: str, last_name: str, phone_number: str, city: str):

需要类似

的内容 /users?email=a&pwd=b&first_name=c...

尝试使用Pydantic models,这样Fastapi将在请求中等待正文。

更多推荐

Axios POST请求错误,来自React的422(无法处理的实体)

本文发布于:2023-10-11 23:54:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483197.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   错误   POST   Axios   React

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!