question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failed to parse header with github fetch polyfill library, response.headers.map won't work correctly

See original GitHub issue

Issue Description I have a react native project, pure react native project for android, it has been under development for few months, it works well until i re-installed packages recently.

After i re-install node_modules with npm install, the network module won’t work correctly anymore. It will failed to parse the response’s headers correctly, below code will get undefined let {'access-token' : accessToken, client, uid, 'token-type':tokenType} = response.headers.map;

Where the bug is I dig into the code, and found the root cause is in react-native/node_modules/whatwg-fetch/fetch.js, line 366:

function parseHeaders(rawHeaders) {
    var headers = new Headers()
    rawHeaders.split('\r\n').forEach(function(line) {// line 366, error happens here, 
                          // it won't split the headers correctly, returns the whole headers as one line. 
                         // it works after i change it to rawHeaders.split('\n').forEach(....){
      var parts = line.split(':')
      var key = parts.shift().trim()
      if (key) {
        var value = parts.join(':').trim()
        headers.append(key, value)
      }
    })
    return headers
  }

So the split(‘\r\n’) does not work correctly, lead to response.headers.map messed up. My app’s authentication depends on tokens from response.headers, it is broken by this error.

What lead to this bug I compared the react-native/node_modules folders before my reinstall, i found there are something changed after i re-install package.

Before i re-install package, there is no react-native/node_modules/whatwg-fetch, after i reinstall it, the react-native/node_modules/whatwg-fetch comes in, then, my app’s network module broken.

Suggestion Remove whatwg-fetch, or fix the split bug in fetch library.

My enviroment

react-native-cli: 1.0.0
react-native: 0.34.1

"react": "15.3.1",
 "react-native": "^0.34.1",
node v6.2.2

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
fsoncommented, Jan 3, 2017

whatwg-fetch@1.1.0 and newer split the headers by \r\n as per spec. This behavior was introduced in https://github.com/github/fetch/commit/57565a1ba1e563e4424486cb88ea541ac892381b.

The XMLHttpRequest.getAllResponseHeaders() method in React Native used to return the headers joined by \n, but this was fixed in v0.36.0 (https://github.com/facebook/react-native/commit/24c72f513e48f0bdc40820b43262328fe2c301d4). A workaround for older React Native versions is to add a dependency on whatwg-fetch@1.0.0 which forces that exact version to be installed.

0reactions
hramoscommented, May 25, 2017

Closing this issue because it has been inactive for a while. If you think it should still be opened let us know why.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A light-weight module that brings the Fetch API to Node.js
Default Headers; Custom Agent; Custom highWaterMark; Insecure HTTP Parser ... run browser-specific Fetch polyfill, why not go from native http to fetch API ......
Read more >
Fetch breaks with update to 3.0.0 · Issue #656 - GitHub
The fetch-polyfill.js includes the fetch.js file from your repository. This is where it fails: export function Headers(headers) { // row 87 ...
Read more >
A Fetch API "Headers" polyfill. - GitHub
This library chooses the Headers instance as the middle-ground between server and client, and provides functions to convert that instance to primitives and...
Read more >
sindresorhus/ky: Tiny & elegant JavaScript HTTP ... - GitHub
Tiny & elegant JavaScript HTTP client based on the browser Fetch API ... which will be JSON.stringify() 'd and sent in the body...
Read more >
Code working in Chrome with native fetch, but not Safari with ...
Failed to load resource: Request header field Content-Type is not allowed by Access-Control-Allow-Headers. Initially I thought it was something ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found