Skip to content

How can I check if a req has a body without trying to parse it in the first place? #1348

Answered by kettanaito
jjoselv asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, @jjoselv.

When handling requests on the web the only way you can check for a body without reading it is by reading the Content-Length header sent in the response. All the other APIs require you to read the body. It makes sense, as body may be represented as a stream, and you have to read that stream to know if there's anything there at all.

Strictly speaking, this is not an MSW-related question. Is there a particular use case you're trying to achieve with the library? If so, please share it, alongside your attempts to do so (code/reproduction repo).

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@jjoselv
Comment options

@kettanaito
Comment options

@camillecroci
Comment options

@kettanaito
Comment options

Answer selected by kettanaito
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants