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

Empty body is converted to '' #84

Closed
leopiccionia opened this issue May 25, 2022 · 2 comments
Closed

Empty body is converted to '' #84

leopiccionia opened this issue May 25, 2022 · 2 comments
Labels
discussion enhancement New feature or request

Comments

@leopiccionia
Copy link

leopiccionia commented May 25, 2022

While migrating an app to ohmyfetch, one assertion on my test suite started to fail on a rather surprising way: the library return "" for responses with empty body (e.g. typical DELETE responses) instead of null or undefined.

If I follow the documentation, I can see why: the library will try to parse the response as JSON as default and, if it fails, as string. Them a body with length == 0 is parsed as an empty string.

While I can understand this behavior, it sounded counter-intuitive to me to return some value if the server intended to return nothing.

If you think this is by-design and not a bug, feel welcome to close it.

@leopiccionia leopiccionia changed the title DELETE body is DELETE body is empty string May 25, 2022
@leopiccionia leopiccionia changed the title DELETE body is empty string Empty body is converted to '' May 25, 2022
@pi0
Copy link
Member

pi0 commented May 31, 2022

Hi @leopiccionia. Nice spot! You can directly respond null to get native null as the result. But i think we can improve the behavior. Do you mind to share exact curl -Iv [URL] output of your API? What are Content-Type and Content-Length for API routes?

@pi0 pi0 added enhancement New feature or request discussion labels May 31, 2022
@leopiccionia
Copy link
Author

It's for a third-party REST API that requires an API key, etc. So it's hard to setup a proper reproduction. My source code is here, though.

For context, it's a DELETE REST endpoint, that returns nothing on success (204 No Content) or a JSON object on failure.

I could extract the following headers from $fetch.raw:

{
  'access-control-allow-origin': '*',
  connection: 'close',
  'content-type': 'application/json; charset=UTF-8',
  date: 'Sat, 04 Jun 2022 16:27:11 GMT',
  server: 'nginx'
}

@pi0 pi0 closed this as completed in 9e20440 Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants