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

readBody returns a empty string when no body request is sent #197

Closed
hidekatsu-izuno opened this issue Oct 16, 2022 · 2 comments
Closed

Comments

@hidekatsu-izuno
Copy link

I sent no body request. Then readBody returns empty string. I can also send empty json string and these can not be distincted.

> await (await fetch('/api/data', {method:'POST'})).json()

await useBody() // -> ''

Shouldn't useBody return undefined when no body request sent ?

@hidekatsu-izuno hidekatsu-izuno changed the title readBody returns empty string when no content is sended. readBody returns a empty string when no body request is sent Oct 16, 2022
@tobiasdiez
Copy link
Contributor

I had a look at this, and I don't think its possible to detect the difference between no body and an empty body (I'm not sure even if there is a difference in the actual http request). Both cases seem to result into a IncomingMessage with a buffer of length 0, see https://github.com/unjs/h3/pull/201/files#diff-27a4e7df4add81d2de697f2623191acfe289d32253fc378a3edc0c1a3f77524cR30-R51 for test cases.

This behavior is also consistent with https://github.com/stream-utils/raw-body/blob/5e59d0a9b214c2c6c4cfb3d6cdb802b2cfa3c0c8/test/index.js#L121-L135 an empty stream is converted to a buffer of length 0.

@pi0
Copy link
Member

pi0 commented Oct 18, 2022

Fixed by 6a0abf0 in 0.8.x. I think we shall respect content-length at least and early return if non is specified with undefined value which is distinguishable from empty string (JSON)

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

3 participants