HTTP Event Cloud Function: request body value is undefined

后端 未结 1 1895
滥情空心
滥情空心 2021-01-23 22:08

When sending {\"identifiant\": \"iJXB5E0PsoKq2XrU26q6\"} to the below Cloud Function, I cannot get the identifiant value in the request body and it wil

相关标签:
1条回答
  • 2021-01-23 22:28

    Unlike a Callable function, the body of a request is not parsed automatically and needs to be parsed before you can use it.

    In addition, json(...) will call end() internally so you don't need both. Also make sure that you don't call end(), send(), json(), etc. multiple times, as this will lead to errors.

    const jsonParser = require('body-parser').json();
    
    exports.meusCandidatos = functions.https.onRequest((req, res) => {
        jsonParser(req, res, (err) => {
          if (err) {
            res.status(500).json({error: err.message});
            return; // stop here
          }
    
          const identifiant = req.body.identifiant;
    
          if (!identifiant) {
            res.status(500).json({error: 'PROBLEMAS NO REQUEST'});
            return; // stop here
          }
    
          // continue
          res.status(200).json({ status: 'ok' });
      })
    });
    
    0 讨论(0)
提交回复
热议问题