Please try this configuration with this request:
curl -X POST -d "{\"origin\":\"202.37.75.130\"}" -H "Content-Type:
application/json" <API Invocation URL>

<api xmlns="http://ws.apache.org/ns/synapse"; name="test"
context="/testScriptMediator">
   <resource methods="POST">
      <inSequence>
         <loopback></loopback>
      </inSequence>
      <outSequence>
         <payloadFactory media-type="json">
            <format>
           {
          "origin":"$1"
          }
          </format>
            <args>
               <arg evaluator="json" expression="$.origin"></arg>
            </args>
         </payloadFactory>
         <script language="js">
var payload = mc.getPayloadJSON();
payload["IP_Address"] = payload["origin"];
delete payload.origin;
mc.setPayloadJSON(payload);
        </script>
         <send></send>
      </outSequence>
   </resource>
</api>

On Fri, Jan 9, 2015 at 11:07 AM, Keerthika Mahendralingam <
[email protected]> wrote:

> Hi Maz,
>
> You can use payload factory mediator to mediate the payload of the message.
>
> I tried as follows. It worked for me:
>
> <api name="Test" context="/test">
>       <resource methods="POST">
>          <inSequence>
>             <loopback/>
>          </inSequence>
>          <outSequence>
>             <payloadFactory media-type="json">
>                <format>{ "IP_Address": "$1"}</format>
>                <args>
>                   <arg evaluator="json" expression="$.origin"/>
>                </args>
>             </payloadFactory>
>             <send/>
>          </outSequence>
>       </resource>
>    </api>
>
> Request:
>
> curl -X POST -d "{\"origin\":\"202.37.75.130\"}" -H "Content-Type:
> application/json" http://192.168.0.102:8280/test
>
> Response:
>
> { "IP_Address": "202.37.75.130"}
>
>
> Thank you,
>
> On Thu, Jan 8, 2015 at 4:43 PM, Buddhima Wijeweera <[email protected]>
> wrote:
>
>> Hi Maz,
>>
>> There's a problem with the JS script. It should be like follows:
>>
>>  <script language="js">var older = mc.getPayloadJSON(); var
>> newer={"IP_Address":older['origin']}; mc.setPayloadJSON(newer);</script>
>>
>>
>> I tried the following API (with ESB 4.8.1) to make the situation quite
>> similar to yours :
>>
>> <api name="Info" context="/info">
>>       <resource methods="POST" url-mapping="/ip">
>>          <inSequence>
>>             <log>
>>                <property name="STATUS" value="Message at In-Sequence"/>
>>             </log>
>>             <loopback/>
>>          </inSequence>
>>          <outSequence>
>>             <log>
>>                <property name="STATUS" value="Message at Out-sequence"/>
>>             </log>
>>             <script language="js">var older = mc.getPayloadJSON();var
>> newer={"IP_Address":older['origin']};mc.setPayloadJSON(newer);</script>
>>             <respond/>
>>          </outSequence>
>>       </resource>
>>    </api>
>>
>>
>> Following image shows the request and the response:
>>
>>
>> ​
>>
>>
>>
>> For your reference I would like to point out following link:
>> http://www.w3schools.com/js/js_json.asp
>>
>> Thank You!
>>
>>
>>
>> On Thu, Jan 8, 2015 at 12:47 PM, Malaka Silva <[email protected]> wrote:
>>
>>> Hi Maz,
>>>
>>> Since you need to generate payload dynamically you can use script
>>> mediator for that purpose.
>>>
>>> On Thu, Jan 8, 2015 at 12:23 AM, Maz Lakadia <[email protected]>
>>> wrote:
>>>
>>>> I will be mapping resources that are in a particular format into
>>>> another format. This means I will have a lot of template mappings that I
>>>> could load into script mediator and let it use the appropriate one for the
>>>> task. I am unsure of how I could do that will payload factory mediator. Do
>>>> you have any clues?
>>>>
>>>> ------------------------------
>>>> Date: Wed, 7 Jan 2015 09:07:13 +0530
>>>> Subject: Re: [Dev] Payload mediation of JSON message using Script
>>>> Mediator
>>>> From: [email protected]
>>>> To: [email protected]
>>>> CC: [email protected]; [email protected]
>>>>
>>>>
>>>> What is the real motivation you to use script mediator instead of
>>>> payload factory?
>>>>
>>>> You can easly construct jaon payload with payload factory
>>>>
>>>> On Wednesday, January 7, 2015, Awanthika Senarath <[email protected]>
>>>> wrote:
>>>>
>>>> Hi Maz,
>>>>
>>>> Can you follow the format at [1] and see whether it works? This
>>>> explains the sample 351 in ESB.
>>>>
>>>> [1]
>>>> https://docs.wso2.com/display/ESB480/Sample+351%3A+Inline+script+mediation+with+JavaScript
>>>> <https://docs.wso2.com/display/ESB480/Sample+351:+Inline+script+mediation+with+JavaScript>
>>>>
>>>> On Wed, Jan 7, 2015 at 4:03 AM, Maz Lakadia <[email protected]>
>>>> wrote:
>>>>
>>>> Hi, I'm using script mediator to mediate the response payload from my
>>>> server. The unmediated response is:
>>>>
>>>>             {"origin":"202.37.75.130"}
>>>>
>>>> I then add in a mediator via source code, that should change "origin"
>>>> to "IP_Address":
>>>>
>>>>             <outSequence>
>>>> <script language="js"><![CDATA[
>>>> old_payload = mc.getPayloadJSON();
>>>> payload["IP_Address"] = old_payload["origin"];
>>>> mc.setPayloadJSON(payload);
>>>> ]]></script>
>>>>                 <filter source="get-property('CORS_ORIGIN')" regex=".+">
>>>>                     <then>
>>>> <property name="Access-Control-Allow-Origin"
>>>> expression="get-property('CORS_ORIGIN')" scope="transport"/>
>>>>                         <property name="Access-Control-Expose-Headers"
>>>> value="Assertion,X-Jwt-Assertion,X-User-Name,X-User-Domain"
>>>> scope="transport"/>
>>>>                     </then>
>>>>                 </filter>
>>>>                 <send/>
>>>>             </outSequence>
>>>>
>>>> However, my response remains unchanged (same as the unmediated
>>>> response). I do not know how I can fix this. Is there an error I am making?
>>>>
>>>> Thanks,
>>>> Maz
>>>>
>>>> _______________________________________________
>>>> Dev mailing list
>>>> [email protected]
>>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Awanthika Senarath
>>>> Software Engineer, WSO2 Inc.
>>>> Mobile: +94717681791
>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Sent from Gmail Mobile
>>>>
>>>> _______________________________________________
>>>> Dev mailing list
>>>> [email protected]
>>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>>
>>>>
>>>
>>>
>>> --
>>>
>>> Best Regards,
>>>
>>> Malaka Silva
>>> Senior Tech Lead
>>> M: +94 777 219 791
>>> Tel : 94 11 214 5345
>>> Fax :94 11 2145300
>>> Skype : malaka.sampath.silva
>>> LinkedIn : http://www.linkedin.com/pub/malaka-silva/6/33/77
>>> Blog : http://mrmalakasilva.blogspot.com/
>>>
>>> WSO2, Inc.
>>> lean . enterprise . middleware
>>> http://www.wso2.com/
>>> http://www.wso2.com/about/team/malaka-silva/
>>> <http://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
>>>
>>>
>>
>>
>> --
>> Buddhima Wijeweera
>> Software Engineer; WSO2 Inc.; http://wso2.com ,
>>
>> Mobile: +94 71 427 9966
>> Email: [email protected]
>> Blog:   https://buddhimawijeweera.wordpress.com
>> GitHub Profile: https://github.com/Buddhima
>>
>> _______________________________________________
>> Dev mailing list
>> [email protected]
>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>
>>
>
>
> --
> <[email protected]>
> Keerthika Mahendralingam
> Associate Software Engineer
> Mobile :+94 (0) 776 121144
> [email protected]
> WSO2, Inc.
> lean . enterprise . middleware
>
> _______________________________________________
> Dev mailing list
> [email protected]
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>


-- 
Shakila Sivagnanarajah
Associate Software Engineer
Mobile :+94 (0) 770 760240
[email protected]
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to