Hosted target NodeJS proxy - JSON data length error

I am using a hosted target NodeJS Apigee proxy in my project. The below code is behaving inconsistent, getting request body length as '0' when using valid JSON for the POST method. However, sometimes it's returning the correct length of the payload. But I am not certain why it's working that way, I have tested in my npm environment, it worked, also tested in my trial Apigee account, it worked there too.

Can you please post your suggestions.

in index.js

===========

const express = require('express');

const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: true}));

require('./router/testRouter')(app);

in testRouter.js

===============

app.route('/testAPI/v1/test').post((req, resp)=> {

var reqBody = req.body;

console.log("length ===" +Object.keys(reqBody).length);

if(Object.keys(reqBody).length > 0){

//to do

} else{

//to do

}

0 1 121
1 REPLY 1

I have tested in my npm environment, it worked, also tested in my trial Apigee account, it worked there too.

I'm not clear. In which environment is it NOT working?

When a problem is intermittent, it's really difficult to fix or solve, unless you have a reliable way of reproducing it. I'd suggest that you explore to figure out if there are specific circumstances that lead to the problem. Maybe the shape of the JSON, the length of the JSON is really large, or ... something else.