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($.)"/>
2. Within the script mediator, get the payload (as a string) by doing this:
var pl_string = mc.getProperty("JSONPayload");
3. Now have to convert the string into a JS object: var pl =
JSON.parse(pl_string);
4. 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"
Hope this helps,
Maz
From: [email protected]
To: [email protected]; [email protected]
Date: Mon, 12 Jan 2015 08:54:09 +1300
Subject: Re: [Dev] getPayloadJSON returning an empty object
I'm using the ESB that comes with WSO2 API Manager 1.7.0.
The Formatter is: class="org.apache.axis2.json.JSONMessageFormatter" The
Builder is: class="org.apache.axis2.json.JSONBuilder"
Date: Fri, 9 Jan 2015 09:25:53 +0530
From: [email protected]
To: [email protected]
Subject: Re: [Dev] getPayloadJSON returning an empty object
Also specify what is the ESB version used? 4.9.0 M4 or 4.8.1?
On Fri, Jan 9, 2015 at 7:54 AM, Asanka Dissanayake <[email protected]> wrote:
Hi Maz,
What is the JSON builder and formatter you are using? You can find them in
<Carbon_Home>/repository/conf/axis2/axis2.xml under Formatters and Builders.
Thanks,
Asanka
On Thu, Jan 8, 2015 at 8:14 PM, Bhathiya Jayasekara <[email protected]> wrote:
Hi Maz,
I quickly tried out your config and it worked fine for me. I'm copying my full
config here for your reference.
<api xmlns="http://ws.apache.org/ns/synapse" name="jsonTest" context="/json">
<resource methods="POST"> <inSequence> <loopback></loopback>
</inSequence> <outSequence> <script language="js">var older =
mc.getPayloadJSON(); var newer=older;
mc.setPayloadJSON(newer);</script> <send></send> </outSequence>
</resource></api>
Request:curl -X POST -d "{\"origin\":\"202.37.75.130\"}" -H "Content-Type:
application/json" http://10.113.56.222:8281/json
Response:{"origin":"202.37.75.130"}
Hope this helps.
Thanks,Bhathiya
On Fri, Jan 9, 2015 at 3:55 AM, Maz Lakadia <[email protected]> wrote:
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? Is this happening because the Payload
is actually a Java object rather than a JSON object?
Thanks,
Maz
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev
--
Bhathiya JayasekaraSoftware Engineer,WSO2 inc., http://wso2.com
Phone: +94715478185LinkedIn: http://www.linkedin.com/in/bhathiyajTwitter:
https://twitter.com/bhathiyaxBlog: http://movingaheadblog.blogspot.com
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev
--
Asanka Dissanayake
Software EngineerWSO2 Inc. - lean . enterprise . middleware | wso2.com
email: [email protected], blog: cyberwaadiya.blogspot.com,
asankastechtalks.wordpress.com mobile: +94 71 8373821
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev
--
Best Regards,
Malaka Silva
Senior Tech Lead
M: +94 777 219 791Tel : 94 11 214 5345
Fax :94 11 2145300 Skype : malaka.sampath.silvaLinkedIn :
http://www.linkedin.com/pub/malaka-silva/6/33/77Blog :
http://mrmalakasilva.blogspot.com/
WSO2, Inc. lean . enterprise . middleware
http://www.wso2.com/
http://www.wso2.com/about/team/malaka-silva/
Save a tree -Conserve nature & Save the world for your future. Print this email
only if it is absolutely necessary.
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev