2

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?

Hairi
  • 3,318
  • 2
  • 29
  • 68
  • 1
    You can pass `strict: false` in the options, then the parsing is the same as `JSON.parse` https://github.com/expressjs/body-parser#strict – cbr Sep 20 '21 at 20:54
  • @cbr You are absolutelly right! Thank you! BTW, any Idea why have they left it `true` / `strict` by default? – Hairi Sep 20 '21 at 20:59
  • (although even with that, it's still worth asking yourself why you're sending `null` at all, because the old "just because you can, doesn't mean you should" almost certainly applies here) – Mike 'Pomax' Kamermans Sep 20 '21 at 21:04
  • @Mike'Pomax'Kamermans May be you are tright, may be I shouldn't send primitve values, but Im trying to learn something here. So why should I not? What if I want to set a property to null? POST /users/Chuck Norris/inventory and then in the body I would say `null` instead of listing all inventory items with `null` values. – Hairi Sep 20 '21 at 21:10
  • No idea: without a description of why you think you need to send `null`, there is very little to comment on, other than the fact that if you _are_ sending just the text string `null` and treating it as a JSON payload, that's almost certainly not a good way to do whatever it is you're trying to do. – Mike 'Pomax' Kamermans Sep 20 '21 at 21:11
  • @Mike'Pomax'Kamermans I edited my previous comment with a use case – Hairi Sep 20 '21 at 21:13
  • 1
    You wouldn't, you'd create dedicated route handling for that, like `/users/{...}/inventory` that you'd call without a payload using the DELETE verb, not GET or POST ([different verbs map to different express `app` functions](https://expressjs.com/en/starter/basic-routing.html), so you can have the same route, but one for `app.get`, one for `app.post`, etc). – Mike 'Pomax' Kamermans Sep 20 '21 at 21:15
  • @Mike'Pomax'Kamermans Make sense – Hairi Sep 20 '21 at 21:15

0 Answers0