set variable in request content using javascript policy

Dear All,

In my flow, i have a javascript policy which gets a flow variable, modify the value and setings the new value in a field in request (which will overwrite preexisting value). Below is my javascript policy which set the variable. And then nodejs script will take request content and read the variable to prcess further in node.js.

But when I ran, i am getting 400:Bad request. Can anyone please help me to sort this out.

var changeVal = context.getVariable("flowVar1");

var req = JSON.parse(context.getVariable("request.content"));

req.field1 = changeVal;

context.setVariable("request.content", JSON.stringify(req));

When I checked trace, found that request body doesnt exists in Request sent to target server but surprisingly request content body is available in Target Request Flow Started (javascript set value policy) with changed value.

Any help would be appreciated!

Thanks

Joydeep

0 4 10.6K
4 REPLIES 4

This is a bug with apigee trace with node target , Apigee trace does not show request parameters for node target.

You can't see headers , body etc.

apigee-nodetarget-trace.png

I am not sure if there is any bug logged for it with apigee. @Dino can provide info on this.

For your implementation be assured that request content is changed and node is receiving modified request.

There might be one issue , if you are changing request payload ,please also change content-length accordingly.

var req = JSON.parse(context.getVariable("request.content"));
req.key = "changeVal";
context.setVariable("request.content", JSON.stringify(req));
context.setVariable("request.header.content-length",req.length);

You can verify request in node either through Node.js log from management UI with console log or reverting request body in response.

const express = require('express');
const app = express();
app.use(express.bodyParser());


app.post('/post',function(req,res){
   res.status(200);
   console.log(req.body);
   res.send(req.body);
});

There was no mention of any issue you are facing apart from missing request parameter in trace.I hope this will help.

Thank you for looking into this.

I have added this - context.setVariable("request.header.content-length",req.length);

Earlier I was getting 400:bad request, now no error but req.body is coming as blank , seen in node.js log.

You are using bodyParser , right ? I have created a simple proxy to check this out and i was able to see modified request body in console as well as in response.

Yes, am using body parser. but its not working for me.

app.use(bodyParser.json());