body-parser
body-parser copied to clipboard
Support for checking empty JSON body.
Requirement
Let the JSON body parser package raise an error if the payload is empty. This middleware maybe added for certain POST
or PUT
requests.
Solution
Check for an empty string and raise a new error named as createEmptyBodyError
which is similar to function createStrictSyntaxError
in parse
function at lib/tpyes/json.js
. This is done when the option of strict checking is set to true
- the default.
Notes
- When the body is empty, then
length
is not set correctly atlib/read.js
, ifContent-Length
is not passed in the request. However, adding a check forContent-Length
header may conflict prior installations.* - For requests that rely on a payload, a 'strict' setting should mean a valid and a non-empty JSON payload. In other words, an extra
options
value to test for empty/non-empty payloads may not be required.
-
- For my purpose, I have a middleware before JSON body parser that looks for multiple headers including
Content-Length
for this requirment.
- For my purpose, I have a middleware before JSON body parser that looks for multiple headers including
Hi @chainhead, can you write more about your use case? Is there a reason this module should be doing this, and not some other middleware specifically for the purpose?
One other small note, this PR also includes unrelated style changes, making it more difficult to tell what is being changed. Can you remove those?