wso2wso2-api-managerwso2-esb

Modifying Server Response Payload JSON


I'm modifying a response sent by a server. To do this I've set up a test API that is meant to just send back the IP, in a format like this:

{
  "origin": "202.37.75.130"
}

My unmodified code gives me back what I want.

What I wish to do as a test is to change the name of "origin" to "IP Address", hopefully getting a response like this:

{
  "IP Address": "202.37.75.130"
}

To do this I've modified my outSequence section of the code, to be this (all I've added is the PayloadFactoryMediator section, the filter was there before):

<outSequence>
  <payloadFactory media-type="json">
    <format>{
      "IP Address" : {
      "$1"
      }
      }
    </format>
    <args>
      <arg evaluator="json" expression="$.origin"/>
    </args>
  </payloadFactory>
  <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, when I curl to the API as before I get a response:

curl: (52) Empty reply from server

Can anybody tell me what I may have done wrong?


Solution

  • I tried your scenario by creating a proxy service for the json service available at "http://ip.jsontest.com/" and I got the expected result. But I used the same "payloadFactory" configuration of yours. Check my full configuration below.

    <?xml version="1.0" encoding="UTF-8"?>
    <proxy xmlns="http://ws.apache.org/ns/synapse"
           name="ipservice"
           transports="https,http"
           statistics="disable"
           trace="disable"
           startOnLoad="true">
        <target>
            <outSequence>
                <payloadFactory media-type="json">
                    <format>{
                        "IP Address" : {
                        "$1"
                        }
                        }
                    </format>
                    <args>
                        <arg evaluator="json" expression="$.ip"/>
                    </args>
                </payloadFactory>
                <send/>
            </outSequence>
            <endpoint>
                <address uri="http://ip.jsontest.com/"/>
            </endpoint>
        </target>
        <description/>
    </proxy>
    

    I tried the above configuration with the filter tag also. It works fine. So there is no error in the above configuration. Please check your JSON service and other configurations.

    I called the service using the following command.

    curl -v -X GET "http://localhost:8280/services/ipservice"