Search code examples
wso2wso2-integration-studio

Call Mediator with Script Mediator


I'm looking for an API with Call Mediator. Then I want to process the data coming with Script Mediaor and generate output with Respond. If I don't use Script Mediator, my stream will work. But when I want to use Script Mediaor I get the following error.

Unexpected character in preface '{' (code 123); expected '<' in [row,col {unknown source}]: [1,1]

<?xml version="1.0" encoding="UTF-8"?>
<api context="/x" name="x" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="GET">
        <inSequence>
            <call>
                <endpoint>
                    <http method="get" uri-template="https://jsonplaceholder.typicode.com/users">
                        <suspendOnFailure>
                            <initialDuration>-1</initialDuration>
                            <progressionFactor>-1</progressionFactor>
                            <maximumDuration>0</maximumDuration>
                        </suspendOnFailure>
                        <markForSuspension>
                            <retriesBeforeSuspension>0</retriesBeforeSuspension>
                        </markForSuspension>
                    </http>
                </endpoint>
            </call>
            <script language="js"><![CDATA[var x = mc.getPayloadJSON();

if  (x.id == "1")
mc.setPayloadJSON('{"s":"success"}');
else 
mc.setPayloadJSON('{"s":"error"}');]]></script>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

Thanks in advance.


Solution

  • The Script Mediator is a content-aware mediator and when you add a content-aware mediator to the mediation flow the message will be built(Binary message converted into a readable format) and brought into the mediation flow. This happens through message builders. From the error snippet you shared, it seems you are receiving a JSON message as a response. So there can be two reasons for this error.

    1. As Arunan mentioned, your backend may not be sending the correct Content-Type as a header with the response.
    2. The correct message builders are not set in the transport configurations to match the Content-Type header the backend is transmitting.

    If you take the second case, first make sure a message builder is set for the default JSON content-type application/json in the <SERVER_HOME>/conf/axis2/axis2.xml. If it's there check if your backend is sending a JSON variation of a default content-type. For example "application/ld+json" etc. If that's the case you need to add a new message builder for this content type. You can refer to this document if you are on MI. If you are on EI refer to this.

    Update After checking your comments, it looks like a straightforward integration. Other than the above two reasons this can be the backend altogether sending a wrong response. Are you using the browser to test this API? If so you may be getting a different response due to additional headers added by the browser. Hence please try removing all the transport headers before the call mediator. Refer to the below. Note the <property action="remove" name="TRANSPORT_HEADERS" scope="axis2"/>

    <property action="remove" name="TRANSPORT_HEADERS" scope="axis2"/>
    <call>
        <endpoint>
            <http method="get" uri-template="https://jsonplaceholder.typicode.com/users">
                <suspendOnFailure>
                    <initialDuration>-1</initialDuration>
                    <progressionFactor>-1</progressionFactor>
                    <maximumDuration>0</maximumDuration>
                </suspendOnFailure>
                <markForSuspension>
                    <retriesBeforeSuspension>0</retriesBeforeSuspension>
                </markForSuspension>
            </http>
        </endpoint>
    </call>