body-parser icon indicating copy to clipboard operation
body-parser copied to clipboard

Support for checking empty JSON body.

Open chainhead opened this issue 4 years ago • 1 comments

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

  1. When the body is empty, then length is not set correctly at lib/read.js, if Content-Length is not passed in the request. However, adding a check for Content-Length header may conflict prior installations.*
  2. 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.

chainhead avatar Sep 26 '19 19:09 chainhead

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?

wesleytodd avatar Sep 27 '19 20:09 wesleytodd