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.

Correct way to identify empty response body

See original GitHub issue

This would solve…

Even if there is no response body, undici handles it by returning a stream with 0 length. Currently the only way to tell if you should be trying to parse the body is response.body._readableState.length, which feels a lot like delving deep into internals.

The implementation should look like…

Additional getter on response would allow to solve this problem without performance overhead:

response.isBodyPresent // getter that checks _readableState.length

I have also considered…

This can also be addressed via documentation, if it is recommended to resolve this in a different way, e. g. by trusting statusCode === 204.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mcollinacommented, Jun 5, 2022

A few notes:

  1. a JSON is at least 2 bytes
  2. a body can be zero length
  3. a content-type of “application/json” guarantees at least two bytes

What I did in the past in those cases is to check the content-type header.

0reactions
mcollinacommented, Jun 5, 2022

Does undici lower-case headers it received in responses?

Yes it does.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the best way to check for empty request Body?
The EOF check can work if you're only checking for the empty body, but I would still also check for other error cases...
Read more >
Tests for empty response body - Help - Postman community
Hi , I have below test: pm.test("Verify the status and name" , function () { var jsonData = pm.response.json(); pm.expect(jsonData[0].name, ...
Read more >
Response with empty body - is this a problem? - jQuery Forum
I call an API on the server side to tell the server that I don't have any body in response. The server replies...
Read more >
5 Ways to Check If an Object Is Empty in JavaScript | Built In
5 Ways to Check If an Object Is Empty in JavaScript · 1. Use Object.keys · 2. Loop Over Object Properties With for…in...
Read more >
Fetch - The Modern JavaScript Tutorial
The browser starts the request right away and returns a promise that ... Response provides multiple promise-based methods to access the body ......
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