POST null body - SyntaxError: Unexpected token # in JSON at position 0
Hi guys, I have a cloud function running Express to serve as a web hook endpoint. My client sends a POST request with a null body value. And the service returns 400 Bad Request with this log :
SyntaxError: Unexpected token # in JSON at position 0 at Object.parse (native) at createStrictSyntaxError (/var/tmp/worker/node_modules/body-parser/lib/types/json.js:157:10) at parse (/var/tmp/worker/node_modules/body-parser/lib/types/json.js:83:15) at /var/tmp/worker/node_modules/body-parser/lib/read.js:121:18 at invokeCallback (/var/tmp/worker/node_modules/raw-body/index.js:224:16) at done (/var/tmp/worker/node_modules/raw-body/index.js:213:7) at IncomingMessage.onEnd (/var/tmp/worker/node_modules/raw-body/index.js:273:7) at emitNone (events.js:86:13) at IncomingMessage.emit (events.js:185:7) at endReadableNT (_stream_readable.js:978:12)
But null is a valid JSON value. Any other correct JSON value seems fine. Express doesn't seem to be reached, so I suppose it is parsed and validated upfront?
As more concrete exemples : response 200 : curl -d '{"foo": true}' -H "Content-Type: application/json" -X POST {endpoint_url} response 400 : curl -d 'null' -H "Content-Type: application/json" -X POST {endpoint_url}
[REQUIRED] Version info
"dependencies": { "body-parser": "^1.18.3", "cors": "^2.8.4", "express": "^4.16.4", "firebase": "^5.5.6", "firebase-admin": "~6.0.0", "firebase-functions": "^2.1.0" }
node: 6
firebase-functions: 2.1.0
firebase-tools:
firebase-admin: ~6.0.0
[REQUIRED] Test case
[REQUIRED] Steps to reproduce
[REQUIRED] Expected behavior
[REQUIRED] Actual behavior
Were you able to successfully deploy your functions?
I found a few problems with this issue:
- I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
- This issue does not seem to follow the issue template. Make sure you provide all the required information.
Hey @didbarbosa, thanks for filing this. This is a known behavior for Google Cloud Functions - it uses https://www.npmjs.com/package/body-parser#strict when parsing the body for requests. This setting will only accept arrays and objects as a valid body value. Unfortunately, this issue lies outside the code in this repo - I've added your issue to our internal bug tracker to raise visibility, and to see if there are reasons for using the strict setting.
Internal bug reference: 64340298
Closing since this issue can't be addressed thru the code in this repo.
Hi, still getting the same annoying behaviour. Any news on this, @joehan ? Thanks a lot
Hey, I ran into a similar issue on Functions. The Functions can not accept invalid json data when "content-type" is application/json. Is there any way to bypass this issue?
@joehan
➜ geme-admin git:(fix/winit-webhook) ✗ curl -X POST \
-H "content-type: application/json" \
-d 'null' https://fh-49d10e67921a045b---ssrgemeadmin-c4oboiisja-de.a.run.app/api/winit/webhook/orderStatusUpdate
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Bad Request</pre>
</body>
</html>
➜ geme-admin git:(fix/winit-webhook) ✗ curl -X POST \
-H "content-type: application/json" \
-d 'invalid json data' https://fh-49d10e67921a045b---ssrgemeadmin-c4oboiisja-de.a.run.app/api/winit/webhook/orderStatusUpdate
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Bad Request</pre>
</body>
</html>
➜ geme-admin git:(fix/winit-webhook) ✗ curl -X POST \
-H "content-type: application/json" \
-d '{ "key": "value" }' https://fh-49d10e67921a045b---ssrgemeadmin-c4oboiisja-de.a.run.app/api/winit/webhook/orderStatusUpdate
fail%