Skip to content

_bodyText has correct JSON but .json() returns completely dfiferent object? #274

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

Closed
dannolan opened this issue Feb 3, 2016 · 3 comments
Closed

Comments

@dannolan
Copy link

dannolan commented Feb 3, 2016

I have a bit of a weird one and I'm trying to figure it out. I've built on this in a react-native project, they use this polyfill.

I make a JSON request and have a body returned, every other request returns the correct (it's to my own server running a rails app) .json() object, but in this returned object I have the following logs:

Full Response Object:

{ _bodyInit: '{"name":"OURUSER","stateChangedAt":"2016-02-02T06:07:21Z","state":"consumed"}',
  _bodyText: '{"name":"OURUSER","stateChangedAt":"2016-02-02T06:07:21Z","state":"consumed"}',
  type: 'default',
  url: '',
  status: 200,
  ok: true,
  statusText: undefined,
  headers: 
   { map: 
      { 'content-type': [ 'application/json; charset=utf-8' ],
        'x-frame-options': [ 'SAMEORIGIN' ],
        'x-runtime': [ '0.048328' ],
        'x-xss-protection': [ '1; mode=block' ],
        server: [ 'nginx' ],
        'content-encoding': [ 'gzip' ],
        'transfer-encoding': [ 'Identity' ],
        'cache-control': [ 'max-age=0, private, must-revalidate' ],
        date: [ 'Wed, 03 Feb 2016 05:28:57 GMT' ],
        'x-request-id': [ '2e56e87f-87bf-4768-81b1-9b93732d2729' ],
        connection: [ 'keep-alive' ],
        'x-content-type-options': [ 'nosniff' ],
        status: [ '200 OK' ],
        vary: [ 'Origin' ] } } }

But if I call data.json()

{ _45: 0, _81: 0, _65: null, _54: null }

But if I call data._bodyText:

{"name":"OURUSER","stateChangedAt":"2016-02-02T06:07:21Z","state":"consumed"}

I've been scratching my head for a few days, I know it's probably a configuration error on a server somewhere but the content types seem to be okay and the headers seem to be okay, I just can't for the life of me figure out what I've done wrong. If this is the wrong project happy to jump down the stack, just haven't seen anyone reporting anything like this before :)

@satya164
Copy link
Contributor

satya164 commented Feb 3, 2016

data.json() returns a Promise, so you need to get the resolved value with then or await. Seems you're directly logging the returned Promise.

@mislav
Copy link
Contributor

mislav commented Feb 3, 2016

Thanks @satya164, you're right. @dannolan: To get either JSON or text representation of the response, you have to use promises:

response.json().then(function(data) {
  console.log(data)
})

// or:
response.text().then(function(responseText) {
  console.log(responseText)
})

It's important that you do not use _bodyText property. The leading underscore indicates that this property is internal to our polyfill and not part of the fetch API. In fact, your code that users our internal property won't work in browsers that have native fetch implementations, such as Chrome and Firefox.

@mislav mislav closed this as completed Feb 3, 2016
@Donhv
Copy link

Donhv commented Jul 20, 2018

ok thanks

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants