Search code examples
wso2wso2-enterprise-integrator

How to add additional information in the response?


Scenario I've integrated an external service in my API. In addition to the response from the external service, I want to add a few more JSON keys and values.

API

<?xml version="1.0" encoding="UTF-8"?>
<api context="/PhoneVerify" name="PhoneVerifi" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>
            <payloadFactory media-type="xml">
                <format>
                    <soapenv:Envelope xmlns:cmpa="http://XXX.XXX.com" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://object.pmd.com/xsd">
                        <soapenv:Header/>
                        <soapenv:Body>
                            <cmpa:verify>
                                <!--Optional:-->
                                <cmpa:userName>XXXXX</cmpa:userName>
                                <!--Optional:-->
                                <cmpa:passwd>XXXXX</cmpa:passwd>
                                <!--Optional:-->
                                <cmpa:request>
                                    <!--Optional:-->
                                    <xsd:cnic>$1</xsd:cnic>
                                    <!--Optional:-->
                                    <xsd:msisdn>$2</xsd:msisdn>
                                    <!--Optional:-->
                                    <xsd:transactionID>$3</xsd:transactionID>
                                </cmpa:request>
                            </cmpa:verify>
                        </soapenv:Body>
                    </soapenv:Envelope>
                </format>
                <args>
                    <arg evaluator="json" expression="$.cnic"/>
                    <arg evaluator="json" expression="$.msisdn"/>
                    <arg evaluator="json" expression="$.transactionID"/>
                </args>
            </payloadFactory>
            <log category="DEBUG" level="full"/>
            <header name="Action" scope="default" value="verify"/>
            <send>
                <endpoint>
                    <address format="soap11" uri="https://XXXXX.com/CMPA/services/CnicMsisdnPairing.CnicMsisdnPairingHttpsSoap11Endpoint/">
                        <suspendOnFailure>
                            <initialDuration>-1</initialDuration>
                            <progressionFactor>1</progressionFactor>
                        </suspendOnFailure>
                        <markForSuspension>
                            <retriesBeforeSuspension>0</retriesBeforeSuspension>
                        </markForSuspension>
                    </address>
                </endpoint>
            </send>
        </inSequence>
        <outSequence>
            <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
                       <payloadFactory media-type="json">
                <format>&#xd;
{&#xd;
    "status" : "success"&#xd;
    "response": "$1"
}</format>
                <args>
                    <arg evaluator="json" expression="$.verifyResponse.return"/>
                </args>
            </payloadFactory>
            <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
            <respond/>
        </outSequence>
        <faultSequence>
                       <payloadFactory media-type="json">
                <format>&#xd;
{&#xd;
    "status" : "failure"&#xd;
}</format>
                <args/>
            </payloadFactory>
        </faultSequence>
    </resource>
</api>

Response from External Service

"verifyResponse":{
    "return" : {
        "@type":"ax21:Response",
        "message":"Duplicate Transaction ID",
        "responseCode":"08",
        "status":"00"
    }
}

Desired Response

{
    "status": "success"
    "response": {
    "@type":"ax21:Response",
    "message":"Duplicate Transaction ID",
    "responseCode":"08",
    "status":"00"}
}

Question My exact question is that when I try to achieve the above problem, I'm getting the response as shown below (basically its wrapped in " "). How can I make achieved the desired response?

{
    "status": "success"
    "response": "{"@type":"ax21:Response","message":"Duplicate Transaction ID","responseCode":"08","status":"00"}"
}

Solution

  • The Payload factory is adding " " since you have defined the argument $1 within " ". If you modify the Payload factory as below you should be able to get the desired output.

    <payloadFactory media-type="json">
        <format>
        {
            "status" : "success",
            "response": $1
        }
        </format>
        <args>
            <arg evaluator="json" expression="$.verifyResponse.return"/>
        </args>
    </payloadFactory>
    

    I have tried the above in the latest Integration Studio(8.1.0), if the above solution doesn't work please specify the MI version you are using.