Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error.response.data is string, while it is a json #378

Closed
velopert opened this issue Jul 16, 2016 · 3 comments

Comments

@velopert
Copy link

commented Jul 16, 2016

axios.post('/api/account/signup', { username, password }, { headers: {'Accept': 'application/json'} })
        .then((response) => {
            console.log(response);
        }).catch((error) => {
            console.log(error.response);
        });

REQ HEADER

POST /api/account/signup HTTP/1.1
Host: localhost:4000
Connection: keep-alive
Content-Length: 29
Accept: application/json
Origin: http://localhost:4000
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:4000/register
Accept-Encoding: gzip, deflate
Accept-Language: ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: connect.sid=s%3AHzDwLfc4dEwawg6OIw9-_Or7AtNNgmRa.nwXRVQiydaGr%2BAs2rnoXsh9JtHTNXhcLkqj3UK7xQqc; key=eyJpc0xvZ2dlZEluIjp0cnVlLCJ1c2VybmFtZSI6InZlbG9wZXJ0In0=

RES HEADER

HTTP/1.1 400 Bad Request
x-powered-by: Express
content-type: application/json; charset=utf-8
content-length: 33
etag: W/"21-l17ts1r+4em82fMYBvsO0w"
date: Sat, 16 Jul 2016 08:32:20 GMT
connection: close

This is what I get in the console:

Object {data: "{"error":"BAD USERNAME","code":1}", status: 400, statusText: "Bad Request", headers: Object, config: Object…}

As you see, data is not a json and it is a string type, so I have to JSON.parse when I have to get the data.

When I was using 0.12, the data was json but now, it is not.
Do I always have to use JSON.parse for the error? or is this something that needs to be fixed?

Thanks!

@mzabriskie

This comment has been minimized.

Copy link
Member

commented Jul 16, 2016

This is definitely a bug. Thanks for reporting.

@mzabriskie

This comment has been minimized.

Copy link
Member

commented Jul 16, 2016

This has been fixed and is available in axios@0.13.1 which has just been published.

@velopert

This comment has been minimized.

Copy link
Author

commented Jul 16, 2016

Wow, that was fast!
Thanks you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.