NOTE: The following approach is only needed until DSS 3.0.0, after DSS 3.0.0, JSON support works without any special content types.

JSON is a popular data format used frequently because of its simplicity and ease of use. WSO2 DSS has built-in support for querying data using JSON. When sending an HTTP request to a data service endpoint, the server identifies the format of the data, i.e. SOAP, JSON etc.., using the “Content-Type” HTTP header. Below contains the content types for some of the well-known formats:

  • text/xml – SOAP
  • application/xml – POX
  • application/json – JSON (mapped notation)
  • application/json/badgerfish – JSON (badgerfish notation)

Here, the two JSON notations represents how the JSON <-> XML conversion happens, this is because, internally in data services, we deal with handling XML element/attributes and namespaces. The “mapped” notation does not support namespaces, only “badgerfish” does. So when using data services, we should use the content type “application/json/badgerfish”. A quick reference on translating XML to badgerfish JSON can be found here [1].

So now for  a small demo, if we deploy the “RDBMSSample” [2] which ships with WSO2 DSS, we will get an HTTP endpoint “http://localhost:9763/services/RDBMSSample/&#8221;. So now we can send an HTTP request to this EPR with a JSON payload. We can use the “curl” tool for this. Below shows a sample run of this.

$ curl --data '{"employeesbynumber":{"employeenumber":{"$":"1002"}}}' http://localhost:9763/services/RDBMSSample --header Content-Type:"application/json/badgerfish" --header SOAPAction:"urn:employeesByNumber"
{"employees":{"@xmlns":{"$":"http:\/\/ws.wso2.org\/dataservice\/samples\/rdbms_sample"},"employee":{"last-name":{"@xmlns":{"$":"http:\/\/ws.wso2.org\/dataservice\/samples\/rdbms_sample"},"$":"Murphy"},"first-name":{"@xmlns":{"$":"http:\/\/ws.wso2.org\/dataservice\/samples\/rdbms_sample"},"$":"Diane"},"email":{"@xmlns":{"$":"http:\/\/ws.wso2.org\/dataservice\/samples\/rdbms_sample"},"$":"dmurphy@classicmodelcars.com"},"salary":{"@xmlns":{"$":"http:\/\/ws.wso2.org\/dataservice\/samples\/rdbms_sample"},"$":"2000.0"}}}}

In the request, we see that, we set the “Content-Type” header to “application/json/badgerfish”, and also we set another HTTP header called “SOAPAction” to “urn:employeesByNumber”, this is basically passed in as the SOAP action to the internal SOAP engine to resolve the operation of the service, and this can be found in the service WSDL. This must be passed here because, the service EPR itself, does not provide any clue about the service operation, it only identifies the service, so the SOAPAction must be provided for the service dispatch to happen properly. The SOAPAction can be omitted, if we mention the service operation in the EPR itself. Then the service endpoint URL should be changed to “http://localhost:9763/services/RDBMSSample/employeesByNumber&#8221;. Below contains a sample run with this configuration.

$ curl --data '{"employeesbynumber":{"employeenumber":{"$":"1002"}}}' http://localhost:9763/services/RDBMSSample/employeesByNumber --header Content-Type:"application/json/badgerfish"
{"employees":{"@xmlns":{"$":"http:\/\/ws.wso2.org\/dataservice\/samples\/rdbms_sample"},"employee":{"last-name":{"@xmlns":{"$":"http:\/\/ws.wso2.org\/dataservice\/samples\/rdbms_sample"},"$":"Murphy"},"first-name":{"@xmlns":{"$":"http:\/\/ws.wso2.org\/dataservice\/samples\/rdbms_sample"},"$":"Diane"},"email":{"@xmlns":{"$":"http:\/\/ws.wso2.org\/dataservice\/samples\/rdbms_sample"},"$":"dmurphy@classicmodelcars.com"},"salary":{"@xmlns":{"$":"http:\/\/ws.wso2.org\/dataservice\/samples\/rdbms_sample"},"$":"2000.0"}}}}

[1] http://ajaxian.com/archives/badgerfish-translating-xml-to-json

[2] http://wso2.org/project/data-services/2.6.2/docs/samples/rdbms_sample.html

Advertisements