ladjs/superagent

react-native: res.body is null, only res.text is working

jonaswindey opened this issue · 20 comments

Using react-native 0.4.1, both my request.get and request.post methods are working, but the res.body is undefined, forcing me to manually JSON.parse the res.text:

request
      .post('http://10.0.0.3:9091/auth')
      .type('json')
      .send({
        email: 'xxx',
        password: 'xxx',
      })
      .end((error, res) => {
        if (res.status === 200) {
          console.log(res.body); // undefined
          var token = JSON.parse(res.text).token; // working
...

When calling this from regular React, it's working perfectly.

Same for me. The res.header['content-type'] is null & res.type is an empty string.

I'm having the same error as well.

@slooker @jonaswindey You can make the change in the PR above & see if that fixes for you as well. It was the cause of my problems.

This solved the problem for me. Nice work. :)

isair commented

I'm having this issue as well using the latest version of react-native and superagent. When I do console.log(res) I see that content-type header is null, but when I do the request using a browser and log the response I can see that Content-Type header exists and is application/json.

However, there is no problem when I do a request to https://api.github.com/users/isair/repos. I'm going to check and see what the server I'm using does different.

I think these are issues with react-native.

I'm still getting this error on firefox with latest version of react and superagent. After long debugging my work around is to add request.set('Accept', 'application/json') or whatever type you expect. This way the browser will treat the response as json even the response content type is something different.

I'm still getting this on react native android. It works fine for iOS, but the res.body is null and res.type is an empty string.

Same issue here

Did anyone get this working? I've tried doing request.set('Accept', 'application/json') using superagent-defaults for example and it still doesn't work. iOS works fine.

@icefoggy Can you share insight if you got it fixed?

Has anyone figured this out yet?

I'm having the same issue now.
Using superagent version 1.5.0

res.body is null.
res.text is full.
the response is text/json, and I'm using a plain get request.

Use my package in NPM called Frisbee
On Dec 9, 2015 12:49 PM, "Jesse" notifications@github.com wrote:

I'm having the same issue now.
Using superagent version 1.5.0

res.body is null.
res.text is full.
the response is text/json, and I'm using a plain get request.


Reply to this email directly or view it on GitHub
#636 (comment)
.

This issue has been closed, because it's not a bug in superagent. Have you tried reporting this bug to react-native authors?

I'm not using React Native

@mienaikoe Sorry, this bug is about react-native only.

But in your comment you say "the response is text/json, and I'm using a plain get request." — try application/json?

@jsergiu Your solution worked for me, used it in react static starter app. Thanks.

same here, if my API return content-type header application/vnd.profiles+json instead of application/json res.body is null and res.text has content.

i have a problem probably like this。
Http.post('/auth_manage/auth/change_auth/') .send({name: values.name}) .send({op: this.state.value}) .set({'X-CSRFToken': Cookie.getItem('csrftoken')}) .type('application/x-www-form-urlencoded') .end((err, res) => { console.log(res) })
this is my code, when i use console.log print res, i got a html file which is my static file, i don't know why, anybody can help me?