0

errorIt's working when making a request with postman for when trying to with axios in reactjs its showing 400 status code

I have already tried - adding removing headers - adding boundary:--foo

handleSubmit(event) {
let data = new FormData();
      data.append("username", this.state.username)
      data.append("password", this.state.password)
      data.append("confirm_password", this.state.confirm_password)
      data.append("email", this.state.email)
      event.preventDefault();
      axios({
         method: 'post',
         url: 'http://localhost:8000/api/account/register/',
         data: data,
      headers:{
         "Content-Type":"application/x-www-form-urlencoded; boundary:XXboundaryXX"
      }
      })
         .then((response) => {
             console.log('bitchhh');

         })
         .catch((response) => {
            //handle error
            console.log(response);
         });
   }

chrome[error code[![postman]3

1
  • It's clear for me. Passwords just don't match (as it can be seen in the images) Commented Jun 1, 2019 at 4:55

1 Answer 1

5

If there are any errors in data, rest framework does not return 2xx status codes it always returns 400 bad request. It means there are errors in request you sent.

Postman is also receiving 400 bad request but it is showing response data (error messages).

axios treats 400 status code as error so you would have to handle it in catch block. If you want to access response sent with errors you can access it from

.catch((err) => {
    //handle error
    console.log(err.response.data);
 });
Sign up to request clarification or add additional context in comments.

Comments

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.