Axios: Unable to get body from axios response

Created on 17 Aug 2017  ·  3Comments  ·  Source: axios/axios

 Summary

I am sending a post request to my from my local to another domain. ( I am using Chrome in dev mode to bypass the Cors issue)

This is my code:

axios('https://..../signup', {
    method: 'POST',
    data: {
    email: '[email protected]',
    password: 'fdsfds',
    confirmPassword: 'fdsfds'
  }

}).then(function(response) {
  console.log('response::', response.data);
})
  .catch(function(error) {
  console.log('ERROR::', error.data);
});

The problem is that I only get the status 400 (Bad Request) as response, whereas if I was to send a the same request via curl cli/postman I would get the status plus a body like:

[
    {
        "param": "email",
        "msg": "Email is not valid"
    },
    {
        "param": "password",
        "msg": "Password must be at least 4 characters long"
    },
    {
        "param": "confirmPassword",
        "msg": "Passwords do not match"
    }
]

But not with axios. This can't be a Cors issue, because I can make a get/post queries fine, I just can't seem to get the body that gets returned with the status.

 Context

  • axios version: "^0.16.2",*
  • Environment: *node v6.9.4, chrome 54, windows 7

Most helpful comment

Never mind console.log('ERROR::', error.response.data); did the job, I hope this will be documented clearly.

All 3 comments

Never mind console.log('ERROR::', error.response.data); did the job, I hope this will be documented clearly.

@samayo Thank you for answering this. It was driving me nuts, using the simple XHR request worked fine as far as getting error responses back, but when using axiosI was wondering why I couldn't get the responses back on error, which you answered perfectly. Agreed it should easier to find in the documentation.

When i send post request using async await syntax if res status is 200 it sends back response body but when response status is 400 it does not send response body. How to get error using async await syntax?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

c0debreaker picture c0debreaker  ·  3Comments

helmus picture helmus  ·  3Comments

Adman picture Adman  ·  3Comments

Baoyx007 picture Baoyx007  ·  3Comments

reggi picture reggi  ·  3Comments