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

Return empty object on 204 No Content... #165

Closed
dandv opened this issue Sep 23, 2016 · 7 comments
Closed

Return empty object on 204 No Content... #165

dandv opened this issue Sep 23, 2016 · 7 comments
Milestone

Comments

@dandv
Copy link
Contributor

dandv commented Sep 23, 2016

...instead of throwing a syntax error due to attempting to JSON parse ''.

let fetch = require('node-fetch');
fetch('http://httpbin.org/status/204').then(res => res.json()).then(object => {
    console.log(object);
}).catch(error => console.error(error));

Demo: http://runkit.com/dandv/node-fetch-204-no-content

Fixing this would attain compatibility with request and got.

More generally, it would be more tolerant to return an empty object if the response is the empty string (regardless of the status code being 204 or other 2xx).

@bedeoverend
Copy link

While I like the idea of an empty object as a JSON response, this moves away from the spec, as they've consciously stayed with throwing an error - IMO it'd be better to stay with the spec for compatibility with native fetch implementations, but if not, should this at least be added as a known difference?

@TimothyGu
Copy link
Collaborator

I agree. Plus, there's also the question whether we should return an empty object, or an empty string, or 0, or false: all of which are legitimate JSON types. @bitinn, thoughts?

@TimothyGu TimothyGu reopened this Nov 6, 2016
@bedeoverend
Copy link

For me, the best thing is to stick with spec - an example use case I'm dealing with is running tests in JSDOM, but now the interface is different between a native client e.g. Chrome and the server side usage. The same applies to isomorphic-fetch, if it relies on this library, users will account for different responses based on if its a server or client environment, which essentially breaks the concept of isomorphic.

@bitinn
Copy link
Collaborator

bitinn commented Nov 7, 2016

I am asking browser vendors for opinions in the same WHATWG thread. But status quo seems clear at this point, neither native implementation/browser fetch polyfill/spec handle 204 empty response.

We should revert our code to match their behaviour in 2.x releases.

@TimothyGu
Copy link
Collaborator

Agreed. I'll prepare a PR.

@bedeoverend
Copy link

Thanks very much for the quick response - really appreciate your efforts on this, it's a great polyfill. Cheers!

TimothyGu added a commit that referenced this issue Nov 23, 2016
This reverts commit 95b5893.

Fixes #165.

Conflicts:
	CHANGELOG.md
	lib/body.js
	test/server.js
	test/test.js
TimothyGu added a commit that referenced this issue Nov 26, 2016
@TimothyGu
Copy link
Collaborator

Fixed in #201.

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

4 participants