Axios POST请求错误,来自React的422(无法处理的实体) [英] axios post request error with 422 (Unprocessable Entity) from react

查看:0
本文介绍了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)
        session.commit()
    
        return f"{email} created..."

反应axios.POST请求

const addUserHandler =  () => {
        console.log(email, pwd, first_name, last_name, phone_number, city);
        axios
            .post('http://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 http://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(无法处理的实体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆