I using Nodejs v14.16.1
, Express 4.17.1
Have set up the following parsing middlewares in my app:
app.use(express.json());
Im trying to pass null
as a body to my request:
> PATCH /api/v1/trusted/erp/users/+359878206067/vehicles/CM0001AM/inspection HTTP/1.1
> Host: localhost:3000
> Content-Type: application/json
> User-Agent: ricotec.herokuapp.com
> Accept: */*
> Content-Length: 4
| null
* upload completely sent off: 4 out of 4 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 Bad Request
< X-Powered-By: Express
< X-Request-Id: f5e6241c-0291-48ae-aee7-8e227cf0a06a
< Content-Security-Policy: default-src 'none'
< X-Content-Type-Options: nosniff
< Content-Type: text/html; charset=utf-8
< Content-Length: 1213
< Date: Mon, 20 Sep 2021 20:38:22 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
* Received 1213 B chunk
* Connection #0 to host localhost left intact
and the response I get is:
SyntaxError: Unexpected token n in JSON at position 0 at JSON.parse () at createStrictSyntaxError (/home/riko/Documents/dev-soft/software/apps/api-v1/node_modules/body-parser/lib/types/json.js:158:10) at parse (/home/riko/Documents/dev-soft/software/apps/api-v1/node_modules/body-parser/lib/types/json.js:83:15) at /home/riko/Documents/dev-soft/software/apps/api-v1/node_modules/body-parser/lib/read.js:121:18 at invokeCallback (/home/riko/Documents/dev-soft/software/apps/api-v1/node_modules/raw-body/index.js:224:16) at done (/home/riko/Documents/dev-soft/software/apps/api-v1/node_modules/raw-body/index.js:213:7) at IncomingMessage.onEnd (/home/riko/Documents/dev-soft/software/apps/api-v1/node_modules/raw-body/index.js:273:7) at IncomingMessage.emit (events.js:327:22) at endReadableNT (internal/streams/readable.js:1327:12) at processTicksAndRejections (internal/process/task_queues.js:80:21)
Now, I know passsing null
as JSON body is not the most common thing to do when writing to a JSON API, but it doesnt change the fact that null
is a valid JSON text as per rfc7159 .
JSON.parse(null)
for example works just fine on browser or Node.js.
Why Express JSON body parser does not accept primitives null
,false
,true
?
These are valid JSON texts, arent they?