INK study
article thumbnail

422 Unprocessable Entity

네트워크 과제 수행 중 422 Unprocessable Entity로 인해 회원가입이 안되는 문제가 발생했다.

@bp.route(f'/createuser', methods=['GET', 'POST'])
def create_user():
    if request.method == 'POST':
        url = f'{endpoint}/users/'
        data = {'username': request.form['username'],
                'password': request.form['password']}
       
        try:
            response = requests.post(url,data=data)
            # response_data = response.json()

            if response.status_code == 201:
                print(f"Request Data: {data}")
                return render_template('createuser.html')

        except requests.exceptions.RequestException as e:
            print(f"Error: {e}")
            if response and response.status_code == 422:
                # Print FastAPI validation error details
                print(response.json())

    return render_template('createuser.html')

 

해결책 : data = data에서 json = data로 변경하면 해결

json 형식으로 데이터를 저장해야하는 부분이 문제였다!

@bp.route(f'/createuser', methods=['GET', 'POST'])
def create_user():
    if request.method == 'POST':
        url = f'{endpoint}/users/'
        data = {'username': request.form['username'],
                'password': request.form['password']}
       
        try:
            response = requests.post(url, json=data)
            # response_data = response.json()

            if response.status_code == 201:
                print(f"Request Data: {data}")
                return render_template('createuser.html')

        except requests.exceptions.RequestException as e:
            print(f"Error: {e}")
            if response and response.status_code == 422:
                # Print FastAPI validation error details
                print(response.json())

    return render_template('createuser.html')
profile

INK study

@ongsimi_

읽어주셔서 감사합니다!