Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

XRequest allows the developer to make an HTTP request to external (3rd party) APIs. The <XRequest> tag can be placed anywhere inside the <Request> tag.

Code Block
languagexml
linenumberstrue
<Request method="GET">
    <Sql id="allMovies" when="$q eq 1">
        SELECT * FROM movie
    </Sql>
    <XRequest id="testReq1" when="$q eq 2" url="http://localhost:7000/testx/v1.1/movies"
                      method="GET" output="true">
        <Header name="Accept" value="application/json" />                
    </XRequest>
</Request>

Attributes

  1. id Used for uniquely identifying the XRequest
  2. url Represents the url endpoint to which the HTTP request is to be made
  3. method The HTTP method (GET/POST/PUT/DELETE)
  4. output Boolean determining whether the response is to be returned (true/false/headers)
  5. classname Name of post processable class

...

  1. name Represents name of the Header/Param
  2. value Represents its value

    Code Block
    languagexml
    linenumberstrue
    <XRequest id="testReq2" url="http://localhost:7000/testx/v1.1/movies"
                    method="POST" output="true" >
        <Header name="Content-Type" value="application/x-www-urlencoded"/>
        <Param name="movie" value="The Godfather" />
        <Param name="rating" value="4" />
    </XRequest> 


 

When making post request with params, it is necessary to use Header Content-Type: application/x-www-urlencoded.

...

Example POST request with JSON

Code Block
languagexml
linenumberstrue
<XRequest id="testReq2" url="http://localhost:7000/testx/v1.1/movies"
                method="POST" output="true" >
    <Header name="Content-Type" value="application/json"/>
    <Body>
        {  
            "p": "The Godfather",
            "q": "4"   
        }
    </Body>
</XRequest>


Example PUT Request with JSON body

Code Block
languagexml
linenumberstrue
<XRequest when="$q eq 5" id="testReq4" url="http://localhost:7000/testx/v1.1/movies/20"
                 method="PUT" output="true" >  
    <Header name="Content-Type" value="application/json"/>
    <Body>
        {  
            "rating": "3"   
        }
    </Body>            
</XRequest>   


Example DELETE Request

Code Block
languagexml
linenumberstrue
<XRequest id="testReq3" url="http://localhost:7000/testx/v1.1/movies/3"
                 method="DELETE" output="true" >               
</XRequest>


Note The HTTP methods used for <Request> and <XRequest> are independent of each other.

...

Request parameters can be accessed inside XRequest by using the $ symbol. The following example shows the parameters title and body being used inside the XRequest body.

Code Block
languagexml
linenumberstrue
<Request method="POST">
    <XRequest id="ExampleRequest" url="https://fcm.googleapis.com/fcm/send"
                  method="POST" output="true">
        <Header name="Content-Type" value="application/json"/>
        <Body>
            {
                "notification": {
                    "title": "$title",
                    "body": "$body"
                }
            }
        </Body>
    </XRequest>
</Request>

XRequest Output

Let's make XRequest to https://jsonplaceholder.typicode.com/todos/1

Code Block
languagexml
linenumberstrue
<Request method="GET">
    <Desc> Example XRequest persist </Desc>
    <XRequest id="testXReq" url="https://jsonplaceholder.typicode.com/todos/1"
                  method="GET" output="true">
    </XRequest>
</Request>

The output of the above Request looks as follows

Code Block
languagejs
linenumberstrue
{
    "testXReq": {
        "id": 1,
        "completed": false,
        "title": "delectus aut autem",
        "userId": 1
    }
}

statusCode holds the HTTP status code received from the API and body consists of the actual payload.

...

This will return the response headers along with the response body as shown

Code Block
languagejs
linenumberstrue
{
    "testXReq": {
        "headers": {
            "Transfer-Encoding": "chunked",
            "Server": "cloudflare",    
            "Date": "Mon, 11 Nov 2019 21:05:56 GMT",
            "Cache-Control": "private, max-age=14400",
            "Etag": "W/\"53-hfEnumeNh6YirfjyjaujcOPPT+s\"",
            "Access-Control-Allow-Credentials": "true",
            "Expires": "-1",
            "Age": "2745",
            "Content-Type": "application/json; charset=utf-8",
            "X-Powered-By": "Express"
        },
        "body": {
            "id": 1,
            "completed": false,
            "title": "delectus aut autem",
            "userId": 1
        },
        "statusCode": 200
    }
}

Response processing

The response returned by XRequest can be given to a post processable class using the classname attribute.

Code Block
languagexml
linenumberstrue
<XRequest id="testReq" url="http://localhost:7000/testx/v1.1/movies"
    method="GET" classname="com.metamug.plugin.ResponseExample" output="true">

The post processable class should implement the ResponseProcessable interface.

Code Block
languagejava
linenumberstrue
public class ResponseExample implements ResponseProcessable {

    @Override
    public Response process(Response response) throws Exception {
        JSONObject responseBody = (JSONObject)response.getPayload();
        // process response here

        return response;
    }    
}