Cannot Change JSON Body Request

Hi,

I'm trying the Apigee Edge API / Development.

I want to change the body request on a POST API on proxyRequest, but unfortunately i can't.

i use this code

print("before");
print(context.proxyRequest.body) //PROXY_REQ_FLOW

var body = context.proxyRequest.body;
body.HelloFromApigee = "HelloFromApigee";
context.setVariable("context.proxyRequest.body", body);

print("after")
print(context.proxyRequest.body) //PROXY_REQ_FLOW

but the result is this

before
{
  "title": "pippo",
  "personId": "1111",
  "pyngCode": "11111",
  "iban": "11111",
  "tipoPagamento": "SDD",
  "insoluti" : [
  		{
  			"codFattura" : "11111",
  			"importoFattura" : "245",
  			"codiceContratto" :"AAAAA",
  			"numRiepilogo" : "123",
  			"dataFattura" : "20/02/2020",
  			"dataRepAddebito" : "29/02/2020"
  		}
  	]
  }
}

after
{
  "title": "pippo",
  "personId": "1111",
  "pyngCode": "11111",
  "iban": "11111",
  "tipoPagamento": "SDD",
  "insoluti" : [
  		{
  			"codFattura" : "11111",
  			"importoFattura" : "245",
  			"codiceContratto" :"AAAAA",
  			"numRiepilogo" : "123",
  			"dataFattura" : "20/02/2020",
  			"dataRepAddebito" : "29/02/2020"
  		}
  	]
  }
}

The body doesn't change, but it should, even if use the bracelet notation instead of dot. The flow is correctly setted. I'm missing something?

0 1 94
1 REPLY 1

Hi @Pasquale Lodise

you can use request.content context variable to achieve what you are trying to do. Please see below link for Flow variables reference documentation page.

https://docs.apigee.com/api-platform/reference/variables-reference#request

so your above code would look something like this.

print("before");
print(context.getVariable("request.content"));


var createreqpbodyval;
var createreqbodyobj = JSON.parse(context.getVariable("request.content"));
createreqbodyobj.Message = "HelloFromApigee";
createreqpbodyval =  JSON.stringify(createreqbodyobj);  


context.setVariable("request.content" , createreqpbodyval);
print("after");
print(context.getVariable("request.content"));

try this and see if you are getting correct results.