...
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 |
---|
language | xml |
---|
linenumbers | true |
---|
|
<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
- id Used for uniquely identifying the XRequest
- url Represents the url endpoint to which the HTTP request is to be made
- method The HTTP method (GET/POST/PUT/DELETE)
- output Boolean determining whether the response is to be returned (true/false/headers)
- classname Name of post processable class
...
- name Represents name of the Header/Param
value Represents its value
Code Block |
---|
language | xml |
---|
linenumbers | true |
---|
|
<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 |
---|
language | xml |
---|
linenumbers | true |
---|
|
<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 |
---|
language | xml |
---|
linenumbers | true |
---|
|
<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 |
---|
language | xml |
---|
linenumbers | true |
---|
|
<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 |
---|
language | xml |
---|
linenumbers | true |
---|
|
<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 |
---|
language | xml |
---|
linenumbers | true |
---|
|
<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 |
---|
language | js |
---|
linenumbers | true |
---|
|
{
"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 |
---|
language | js |
---|
linenumbers | true |
---|
|
{
"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 |
---|
language | xml |
---|
linenumbers | true |
---|
|
<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 |
---|
language | java |
---|
linenumbers | true |
---|
|
public class ResponseExample implements ResponseProcessable {
@Override
public Response process(Response response) throws Exception {
JSONObject responseBody = (JSONObject)response.getPayload();
// process response here
return response;
}
} |