REST Data Services with WSO2

If you know something of WSO2, you will know that you can create easily web services with a database as the backend, also known as data services. These data services by default are called as SOAP services. But WSO2 Data services are prepared to be invoked as REST services.

We will explain how to do it with one example per each HTTP method. But prior to that, you need to have already configured a data source previously. In our example that data source will be called LIBRARY.

In the code section below, you can see a basic example of a data service, configure with our data source and a query sample.

For now, this data service can not do anything by itself. We will need to add an operation or a resource element. And here it will be the difference between having a REST data service or not. The operation element lets us SOAP invocations and the resource lets REST invocations.

But before to create a resource element in the data service you must know some terms to configure it:

  • resource path: the context to call the date services. Here you can also configure query params.
  • resource method: the HTTP method to invoke it.
  • query id: the id of the query associated with the resource.
  • query params: the association between params from the resource and the query.

Now, we can see a resource configured for the previous query. The necessary param for the query getBook, it will be configured as a query param.

To invoke it, we can use the following curl command. Remember book_dataservices is our data service name.

The following is an example of an HTTP Post call.

The main difference to invoke it’s how to pass the query params. For that, we need to create a JSON payload, but this payload must have a specific format. This payload is a JSON object composed of an underscore with the name of the HTTP method (in this case it will be _post) and the resource attribute context (each slash will be translated to an underscore also).

The PUT method will be very similar to the POST method.

We can test it with the following command:

And finally the HTTP Delete resource:

An example of the invocation will be:

It’s very important to be conscientious about the Accept and Content-Type HTTP headers, and in which cases we must use them or not. This can be the difference between that the invocation works correctly or not.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store