1

I use React + Django, when user signup from frontend the axios in React send POST signal to Django, if form is invalid it shows below response

xhr.js:178 POST http://localhost:8000/account/users/ 
signup.js:120 Error: Request failed with status code 400
    at createError (createError.js:16)
    at settle (settle.js:17)
    at XMLHttpRequest.handleLoad (xhr.js:61)

If I do same POST with invalid fields in Django browsable API or Postman I get below error and it is useful to display error to users

{
    "email": [
        "This field may not be blank."
    ],
    "username": [
        "This field may not be blank."
    ]
}

If user type invalid details in form and after form submission, How can I get same error message from axios ?

Please note that if user type correct information in form, axios responds with sufficient details.

Axios function

 axios.post('/account/users/',{data}).then(res=>console.log(res)).catch(err=>console.log(err)

1 Answer 1

3

It's needles to say that the response is exactly the same in both cases, an HTTP response with status 400 (Bad Request) and with the body you see in postman. Your issue is related to how Axios provides you the response. If you want to access the response body when Axios gets an error response, you have it in error.response.data:

axios.post('/account/users/'{data})
.then(res=>console.log(res))
.catch(err=>console.log('Response body', err.response.data)
Sign up to request clarification or add additional context in comments.

1 Comment

When the same is added in try catch block, error occurs. Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.