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

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

Closed
jonaswindey opened this issue Apr 28, 2015 · 20 comments
Closed

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

jonaswindey opened this issue Apr 28, 2015 · 20 comments

Comments

@jonaswindey
Copy link

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.

@rpastorelle
Copy link

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

@slooker
Copy link

slooker commented May 3, 2015

I'm having the same error as well.

@rpastorelle
Copy link

@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.

@slooker
Copy link

slooker commented May 3, 2015

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

frantic pushed a commit to frantic/react-native that referenced this issue May 6, 2015
Summary:
`XMLHttpRequest.getResponseHeader` is case-insensitive, therefor the React-Native implementation needs to mimic this behavior as to not break libraries that are dependent on this.

There is a corresponding issue in `superagent` but this is the root cause (ladjs/superagent#636).
Closes facebook#1138
Github Author: Ryan Pastorelle <rpastorelle@yahoo.com>

Test Plan: Imported from GitHub, without a `Test Plan:` line.
@isair
Copy link

isair commented Jul 21, 2015

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.

@defunctzombie
Copy link
Contributor

I think these are issues with react-native.

@jsergiu
Copy link

jsergiu commented Aug 20, 2015

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.

@icefoggy
Copy link

icefoggy commented Oct 9, 2015

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.

@niftylettuce
Copy link
Collaborator

Same issue here

@niftylettuce
Copy link
Collaborator

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.

@niftylettuce
Copy link
Collaborator

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

@niftylettuce
Copy link
Collaborator

Has anyone figured this out yet?

@mienaikoe
Copy link

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.

@niftylettuce
Copy link
Collaborator

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)
.

@kornelski
Copy link
Contributor

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

@mienaikoe
Copy link

I'm not using React Native

@kornelski
Copy link
Contributor

@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?

@vandernoud
Copy link

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

@MathieuDoyon
Copy link

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.

@NICK-DUAN
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests