Skip to content

Injecting Messages to a RESTful Endpoint

In order to use the Message Injector to inject messages to a RESTful endpoint, you can specify the injector with the required payload and inject the message to the sequence or proxy service as defined below. The sample below shows a RESTful message injection through a proxy service.

Synapse configurations

Following are the integration artifacts that we can used to implement this scenario. See the instructions on how to build and run this example.

<task class="org.apache.synapse.startup.tasks.MessageInjector" group="synapse.simple.quartz" name="SampleInjectToProxyTask" xmlns="http://ws.apache.org/ns/synapse">
    <trigger count="2" interval="5"/>
    <property name="injectTo" value="proxy" xmlns:task="http://www.wso2.org/products/wso2commons/tasks"/>
    <property name="message" xmlns:task="http://www.wso2.org/products/wso2commons/tasks">
        <request xmlns="">
            <location>
                <city>London</city>
                <country>GB</country>
            </location>
        </request>
    </property>
    <property name="proxyName" value="SampleProxy" xmlns:task="http://www.wso2.org/products/wso2commons/tasks"/>
</task>
<?xml version="1.0" encoding="UTF-8"?>
<proxy name="SampleProxy" startOnLoad="true" transports="http https" xmlns="http://ws.apache.org/ns/synapse">
    <target>
        <inSequence>
            <property expression="//request/location/city" name="uri.var.city" scope="default" type="STRING"/>
            <property expression="//request/location/country" name="uri.var.cc" scope="default" type="STRING"/>
            <log>
                <property expression="get-property('uri.var.city')" name="Which city?"/>
                <property expression="get-property('uri.var.cc')" name="Which country?"/>
            </log>
            <call>
                <endpoint key="EP" />
            </call>
            <log level="full"/>
            <respond />
        </inSequence>
        <faultSequence/>
    </target>
</proxy>
<endpoint name="EP" xmlns="http://ws.apache.org/ns/synapse">
    <http method="get" uri-template="http://api.openweathermap.org/data/2.5/weather?q={uri.var.city},{uri.var.cc}&amp;APPID=ae2a70399cf2c35940a6538f38fee3d3" />
</endpoint>

Build and run

Create the artifacts:

  1. Launch Visual Studio Code with the Micro Integrator for VS Code extension (MI for VS Code) installed.

    Info

    Follow the Install Micro Integrator for VS Code documentation for a complete installation guide.

  2. Create an integration project.

  3. Create the proxy service and a scheduled task with the configurations given above.
  4. Deploy the artifacts in your Micro Integrator.

The XML message you injected (i.e., This is a scheduled task of the default implementation.) will be printed in the logs of the Micro Integrator twice, 5 seconds apart.

INFO {org.apache.synapse.mediators.builtin.LogMediator} - Which city? = London, Which country? = UK