Can not get parameter from req.body with POST request in Azure NodeJS AppService

繁伸 松尾 51 Reputation points
2021-03-02T02:27:16.477+00:00

Hi

My Windows Azure NodeJS AppService can not get parameter from req.body. I am using Express 4.17 with body-parser. Every time I post a request with body data (example: {"uid":"12345"}) with server-side code

router.post('/abc', function (req, res) {
var uid = req.body.uid;
});

This code worked normally on local but in Windows Azure NodeJS AppService, uid is always Undefined or Null
How can I fix this problem ? Thank you very much.

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,212 questions
{count} votes

Accepted answer
  1. Ryan Hill 26,856 Reputation points Microsoft Employee
    2021-03-03T04:51:23.057+00:00

    Hi @繁伸 松尾 ,

    Since your code is server side, have you tried remote debugging your application? I did come across https://stackoverflow.com/a/13779626/10987310, which contains an example of using body-parser. I've confirmed this does indeed work on a Windows hosted App Service running NodeJS 12LTS.

    73557-image.png

    I used the following code.

    var express = require('express');  
    var bodyParser = require('body-parser')  
    var router = express.Router();  
      
    var jsonParser = bodyParser.json();  
      
    /* GET users listing. */  
    router.get('/', function(req, res, next) {  
      res.send('respond with a resource');  
    });  
      
    router.post('/one', jsonParser, function(req, res){  
      console.log(JSON.stringify(req.body));  
      res.send(req.body.name);  
    });  
      
    module.exports = router;  
    

    Regards,
    Ryan


0 additional answers

Sort by: Most helpful