News for hackers. Stuff that matters.

Ask HN | http compliance if post with content-length:0

Hi

I have a corner case :-)

A library client do a POST request on my server, with 2 headers : > Content-type: application/json > Content-length: 0

So ATM the server response is 400 (bad request) because json is invalid

So the question is : When the content-length of a request is 0, should we parse the body or not ?

( 3 comments | 3 points | on HN )
Re:Ask HN | http compliance if post with content-length:0 by clementd on Wednesday July 10, 2013 @10:46AM (#6020026)
To my mind, as long as the Content-type header is set, the server should try to parse the body accordingly.

In the case of JSON, an empty body cannot be a JSON representation, but for other formats an empty body can have a meaning.

Re:Ask HN | http compliance if post with content-length:0 by clementd on Wednesday July 10, 2013 @11:11AM (#6020233)
For instance for a plain text content-type, no string != empty string
Re:Ask HN | http compliance if post with content-length:0 by dalke on Wednesday July 10, 2013 @11:32AM (#6020388)
What does it matter?

What you should do is have the error messages be the same for detecting a length 0 submission and of attempting to parse the empty string as JSON.

You can think of the check for length==0 and length==1 as a sort of optimization (no JSON document can be 1 byte long), rather than a corner case.