Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getPayloadJSON returning an empty object

I am doing some very simple script mediation as a test to see if I can change the payload being returned by my server.

My unmediated payload comes out as:

{"origin":"202.37.75.130"}

Then I try doing a very simple check to see whether get and set work for payloads:

<script language="js">
    var older = mc.getPayloadJSON(); 
    var newer=older; 
    mc.setPayloadJSON(newer);           
</script> 

My result from this is:

{"jsonObject":""}

I have done testing that shows that setPayloadJSON() works, which means that my getPayloadJSON must be returning an empty object.

Looking at the log file shows this:

ERROR {org.apache.synapse.mediators.bsf.ScriptMessageContext} -  JSON object is null. {org.apache.synapse.mediators.bsf.ScriptMessageContext}

Is there anyone else having this error?

like image 804
MLak Avatar asked May 26 '26 06:05

MLak


1 Answers

Just in case someone has the same issue, I managed to get mine working.

I had to do a workaround method:

  1. Use property mediator before the script mediator to set a property:

property name="JSONPayload" expression="json-eval($.)"

  1. Within the script mediator, get the payload (as a string) by doing this:

var pl_string = mc.getProperty("JSONPayload");

  1. Now have to convert the string into a JS object:

var pl = JSON.parse(pl_string);

  1. Manipulate the payload as if you'd obtained it using getPayloadJSON().

I know it's a hacked together method, but none of the other ways were working for me so I tried this one.

Some information in case anyone wants to debug it:

I'm using API Manager 1.7.

I set the message builders and formatters to these: Builder class = "org.apache.synapse.commons.json.JsonStreamBuilder" Formatter class = "org.apache.synapse.commons.json.JsonStreamFormatter"

like image 131
MLak Avatar answered Jun 01 '26 20:06

MLak