Failed to parse header with github fetch polyfill library, response.headers.map won't work correctly
See original GitHub issueIssue 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:
- Created 7 years ago
- Comments:9 (8 by maintainers)

Top Related StackOverflow Question
whatwg-fetch@1.1.0and newer split the headers by\r\nas 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 onwhatwg-fetch@1.0.0which forces that exact version to be installed.Closing this issue because it has been inactive for a while. If you think it should still be opened let us know why.