Versions Compared

Key

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

...

For a demonstration of the usage of REST support, see Exposing Data as REST-Style Resources. 

...

Sending and receiving JSON messages

By default, the REST resources or data services defined in WSO2 DSS receives messages and responds to messages in XML format. However, you have the option of specifying the response format when you define your data service/resource. See the topic on output mapping for instructions.

Given below are some guidelines that you need to follow when working with REST resources and JSON messages in WSO2 DSS.

Table of Contents
maxLevel4
minLevel4

Receiving a JSON response from a REST resource

From WSO2 DSS 3.2.0 onwards, the possibility of receiving messages in JSON format is enabled by default. Therefore, you can simply send requests by adding “Accept:Application/json” to the request header, and you will receive the response in JSON. For example, shown below is how you can send a GET request to a resource using CURL:

Code Block
curl -X GET -H "Accept:application/json" http://localhost:9763/services/samples/ResourcesSample.HTTPEndpoint/product/S10_1678

Additionally, if you have the output mapping set to JSON in your resource query as explained here, the JSON response you receive will be according to the JSON template you have defined in the output mapping.

Info

The possibility of receiving JSON responses is enabled by the following settings in the axis2.xml and axis2_client.xml files (stored in the <DSS_HOME>/repository/conf/axis2 directory):

Code Block
<parameter name="httpContentNegotiation">true</parameter>

Sending JSON payloads to a REST resource

If you are sending JSON request payloads for data services REST resources, the JSON payload must be in a specific format for different types of HTTP methods as explained below.

HTTP POST

When you send an HTTP POST request, the format of the JSON object name should be "_post$RESOURCE_NAME", and the child name/values of the child fields should be the names and values of the input parameters in the target query. Given below is an example of a JSON request that sends an HTTP POST request to the "employee" resource path. 

Code Block
{
  "_postemployee": {
    "employeeNumber" : 1,
    "lastName": "Doe",
    "firstName": "John",
    "email": "jdoe@wso2.com",
    "salary": 13500.0
  }
}
HTTP PUT

When you send an HTTP PUT request, the format of the JSON object name should be "_put$RESOURCE_NAME", and the child name/values of the child fields should be the names and values of the input parameters in the target query. Given below is an example of a JSON request that sends an HTTP PUT request to the "employee" resource path. 

Code Block
{
  "_putemployee": {
    "employeeNumber" : 1002,
    "lastName": "Smith",
    "firstName": "Will",
    "email": "will@smith.com",
    "salary": 86500.0
  }
}
JSON-based resource batch requests

When batch requests are enabled for data services resources, resource paths are created with the "_batch_req" suffix. In the payload content, the single request JSON object becomes one of the many possible objects in a parent JSON array object. This will have a name with a single request wrapper object + the "_batch_req". Shown below is a sample batch request for the HTTP POST request shown earlier.

Code Block
{
    "_postemployee_batch_req": {
        "_postemployee": [
            {
                "employeeNumber": 5012,
                "lastName": "Smith",
                "firstName": "Will",
                "email": "will@smith.com",
                "salary": 13500.0
            },
            {
                "employeeNumber": 5013,
                "lastName": "Peter",
                "firstName": "Parker",
                "email": "peter@parker.com",
                "salary": 15500.0
            }
        ]
    }
}