Minggu, 16 April 2017

Convert HTTP GET to POST in OSB REST service

This is very important and useful blog for all who has developed OSB REST API using HTTP GET verb and quickly want to convert into HTTP POST verb. That change may be due to some security reasons in the organization.

The blog will help you to convert HTTP GET operation into HTTP POST. This is 2 minutes job.

Considering you have a REST API services with one GET operation. If you don't have you can follow my previous blog and have the REST API in place.

Below given screen shot shows you the REST API GET operation:


Run the REST service and see the Method. It is GET



In this blog, we will convert GET to POST using very simple steps.

Each REST API have one wadl file that contains the operation name, HTTP verb(GET,POST,DELETE, PUT etc.), request and response parameter information. So this conversion can be done by simply editing the WADL file.

Download sample project from here

Let's see step by step:

Open RestService.wadl file and see the highlighted part that you needs to be edit



<?xml version = '1.0' encoding = 'UTF-8'?>
<application xmlns:soa="http://www.oracle.com/soa/rest" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="http://xmlns.oracle.com/RESTServiceApplication/RESTServiceProject/RestService" xmlns:ns0="http://www.example.org" xmlns="http://wadl.dev.java.net/2009/02">
<doc title="RestService">RestService</doc>
<grammars>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://www.example.org" schemaLocation="../Schemas/Employees.xsd"/>
</xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://oracle.com/mobile/errors" schemaLocation="../Schemas/Error.xsd"/>
</xsd:schema>
</grammars>
<resources>
<resource path="/">
<method name="GET" soa:wsdlOperation="getEmp">
<request>
<param name="deptId" style="query" soa:expression="$msg.request/ns0:deptId" default="" type="xsd:int"/>
</request>

<response status="200">
<representation mediaType="application/xml" element="cns:EmployeesCollection" xmlns:cns="http://www.example.org"/>
<representation mediaType="application/json" element="cns:EmployeesCollection" xmlns:cns="http://www.example.org"/>
</response>
</method>
</resource>
</resources>
</application>
Let's change the highlighted part of WADL

1) Change the method name from GET to POST
2) Chang the request tag like below:

<request>
<representation mediaType="application/xml" element="cns:request" xmlns:cns="http://www.example.org"/>
<representation mediaType="application/json" element="cns:request" xmlns:cns="http://www.example.org"/>
</request>


We can either put two tags or one depending on the choice. If we want to send request in both(XML/JSON) then use both else any one of your choice.

WADL will look like after change

<?xml version = '1.0' encoding = 'UTF-8'?>
<application xmlns:soa="http://www.oracle.com/soa/rest" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="http://xmlns.oracle.com/RESTServiceApplication/RESTServiceProject/RestService" xmlns:ns0="http://www.example.org" xmlns="http://wadl.dev.java.net/2009/02">
<doc title="RestService">RestService</doc>
<grammars>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://www.example.org" schemaLocation="../Schemas/Employees.xsd"/>
</xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://oracle.com/mobile/errors" schemaLocation="../Schemas/Error.xsd"/>
</xsd:schema>
</grammars>
<resources>
<resource path="/">
<method name="POST" soa:wsdlOperation="getEmp">
<request>
<representation mediaType="application/xml" element="cns:request" xmlns:cns="http://www.example.org"/>
<representation mediaType="application/json" element="cns:request" xmlns:cns="http://www.example.org"/>
</request>

<response status="200">
<representation mediaType="application/xml" element="cns:EmployeesCollection" xmlns:cns="http://www.example.org"/>
<representation mediaType="application/json" element="cns:EmployeesCollection" xmlns:cns="http://www.example.org"/>
</response>
</method>
</resource>
</resources>
</application>




Now, open the REST adapter and verify the updates:


Run the project and see the output:


Tidak ada komentar:

Posting Komentar