Skip to content

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

Closed
@dannolan

Description

@dannolan

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

Activity

satya164

satya164 commented on Feb 3, 2016

@satya164
Contributor

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

mislav commented on Feb 3, 2016

@mislav
Contributor

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.

Donhv

Donhv commented on Jul 20, 2018

@Donhv

ok thanks

locked as resolved and limited conversation to collaborators on Oct 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mislav@dannolan@satya164@Donhv

        Issue actions

          _bodyText has correct JSON but .json() returns completely dfiferent object? · Issue #274 · JakeChampion/fetch