Search code examples
middlewarewso2wso2-esb

How can I put an XML mocked payload into a POST request payload sended by a WSO2 ESB send mediator?


I am absolutly new in WSO2 ESB and at this time I have to achive this first simple task but I really don't know from where start.

I have to perform an HTTP POST request towards an external web service. This request should contains a mocked XML document as payload (at this time the XML have to be merely mocked so I think that I have to "write" it somewhere or something like this).

So starting by this minimalistic official tutorial:

https://docs.wso2.com/display/ESB500/Sending+a+Simple+Message

I have done the following operation:

1) I have created a new ESB Solution Project.

2) On the "main" project named SampleServices (at this time I am using the same name of the tutorial) I have registered the endpoint to my external web service (New -> Endpoint).

3) Then on this project I have add a new RESP API (New -> REST API). Differently from the tutorial I have enable POST request instead of GET into the API Resource properties because I need to send a POST request containing a payload instead the tutorial GET request. This is my settings:

enter image description here

4) Then, as done in the tutorial, I have setted the send mediator and on this mediator I have setted the defined endpoint pointing at my external web service that have to handle the POST request and retrieve its payload.

Ok, now my problem is: how can I put a mocked XML document into the body of my POST request sended by my *send mediator**?

Reading this second tutorial: https://docs.wso2.com/display/ESB500/Routing+Requests+Based+on+Message+Content

I know that I can pass the request payload from outside, for example in this second tutorial perform this CURL call that starts the ESB "process":

curl -v -X POST --data @request.json http://localhost:8280/healthcare/categories/surgery/reserve --header "Content-Type:application/json"

This do a call that pass a JSON as payload. In the previous example works fine but it is not good for me.

I have to mock the XML document payload inside my ESB API flow and not take from an external request performed by CURL because in a second implementationstep it will not be mocked but il will come from a previous step in the chain.

So I need a flow like this:

enter image description here

When my API is called it perform a POST request that send a mocked XML payload. Maybe it is have to be putted into the send mediator but I don't know...

How can I put this XML in the payload of the requeste sended by the send mediator? Where have I to define\write it?


Solution

  • Check this sample:

    <?xml version="1.0" encoding="UTF-8"?>
    <api context="/services/users" name="ListUsersAPI" xmlns="http://ws.apache.org/ns/synapse">
      <resource methods="GET" protocol="http" url-mapping="/*">
        <inSequence>
          <payloadFactory media-type="json">
            <format>{ "persons":[{ "person": { "Id":"1", "givenName":"ajith", "lastName":"vitharana", "age":"25", "contactInfos":[ { "InfoId":"1", "department":"1", "contactType":"email", "value":"[email protected]" }, { "InfoId":"2", "department":"1", "contactType":"mobile",
              "value":"111111111" }, { "InfoId":"3", "department":"1", "contactType":"home", "value":"Magic Dr,USA" } ] }}, {"person":{ "Id":"2", "givenName":"shammi", "lastName":"jagasingha", "age":"30", "contactInfos":[ { "InfoId":"1", "department":"1", "contactType":"email",
              "value":"[email protected]" }, { "InfoId":"2", "department":"1", "contactType":"mobile", "value":"2222222222" }, { "InfoId":"3", "department":"1", "contactType":"home", "value":"Magic Dr,USA" } ] } }] }</format>
            <args/>
          </payloadFactory>
          <property action="remove" name="NO_ENTITY_BODY" scope="axis2"/>
          <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
          <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
      </resource>
    </api>