在Hello World应用程序中使用无效JSON的Cloud Functions 400 Bad Request

时间:2018-12-25 22:32:08

标签: node.js firebase express

我有一个hello world cloud功能应用程序,它可以通过 firebase服务在本地运行,也可以在 firebase部署

后在线运行
const functions = require('firebase-functions');

exports.helloWorld = functions.https.onRequest((request, response) => {
 response.send("Hello from Firebase!");
});

我可以在本地打开http://localhost:5000/testing-98b0b/us-central1/helloWorld,没有任何问题,但是如果我使用邮递员发送带有无效主体(如“#”)的帖子,则会崩溃并继续加载并显示以下错误:

enter image description here

error: { SyntaxError: Unexpected token # in JSON at position 0
    at JSON.parse (<anonymous>)
    at createStrictSyntaxError (/usr/local/lib/node_modules/firebase-tools/node_modules/body-parser/lib/types/json.js:158:10)
    at parse (/usr/local/lib/node_modules/firebase-tools/node_modules/body-parser/lib/types/json.js:83:15)
    at /usr/local/lib/node_modules/firebase-tools/node_modules/body-parser/lib/read.js:121:18
    at invokeCallback (/usr/local/lib/node_modules/firebase-tools/node_modules/raw-body/index.js:224:16)
    at done (/usr/local/lib/node_modules/firebase-tools/node_modules/raw-body/index.js:213:7)
    at IncomingMessage.onEnd (/usr/local/lib/node_modules/firebase-tools/node_modules/raw-body/index.js:273:7)
    at IncomingMessage.emit (events.js:182:13)
    at endReadableNT (_stream_readable.js:1094:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
  expose: true,
  statusCode: 400,
  status: 400,
  body: '#',
  type: 'entity.parse.failed' }

firebase部署之后,我也可以打开 https://us-central1-staff-testing-98b0b.cloudfunctions.net/helloWorld 没有任何问题,但是如果我使用邮递员发送带有无效主体(如“#”)的帖子,则会崩溃并返回400错误的请求,并出现以下错误:

enter image description here

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:974:12)

firebase-functions库中是否存在bug?有什么方法可以处理这种无效请求?

0 个答案:

没有答案