getPayloadJSON returning an empty object

折月煮酒 提交于 2019-11-30 23:44:50

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"

Hope https://github.com/asanka88/ForcefulJsonConvertor can help you to solve the problem. And I am sure it will. I wrote this to overcome same issue as yours.

Thanks

Recently I needed to add events to payload json data to send to queue. Below is script working for me.

<property name="JSONPayload" expression="json-eval($.)"/>
          <script language="js">
                    <![CDATA[
                    var pl_string = mc.getProperty("JSONPayload");
                    var newPayload="{\"event\": " + pl_string + "}";
                    mc.setPayloadJSON(newPayload);
                 ]]>
            </script>
<log level="full" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!