Monday, February 27, 2017

Oracle ORDS rest service receiving xml data

I have a project where I will need to provide a web service which can accept xml data, in ORDS, it's very easy to create a web service to accept Json data but somehow it's a different story when it come to xml data. Basically ORDS can parse the json data very well but it will not parse the xml itself. I struggled a little bit before I was able to make it work(thanks to Mike Kutz's help -- see the detail here https://community.oracle.com/thread/4020782).

It turns out that ORDS will treat the xml data only as BLOB, So we will need to parse the data inside our plsql.

Here is how it works:

1. create a web rest service(PUT operation in my case, application/xml in content type..) the same way you create a json based rest web service.
2. in the plsql, use :body to refer the incoming xml data from the client.
3. parse the xml data and use whatever needed in your plsql.

Here is the protocol of the plsql code:

declare
     bb varchar2(50);
     cc varchar2(40);
     aa varchar2(4000);
begin
    aa:=UTL_RAW.CAST_TO_VARCHAR2(:body); ---use :body to refer the xml data. and convert it to varchar2, :body will be BLOB type
SELECT xt.* into bb,cc
     FROM
           XMLTABLE('/Data'
              PASSING aa
              COLUMNS
                "RG_GROUP"   VARCHAR2(40)  PATH 'rs_group',
                "Status"    VARCHAR2(10) PATH 'status'
              ) xt;
-- parsing the xml data to get the data you wanted based on the xml format
     insert into mytrace values(bb||'/'||cc); --- do whatever you want here.
end;

in my case, the xml data is something like this:

<Data>
<rs_group>xxxx-prd</rs_group>
<status>Success</status>

</Data>


So the key here is that ORDS will accept the xml data as BLOB data and then we can refer it as :body in our plsql.

2 comments:

  1. Hi There
    I see a reference to :body in many docs - but how is it routed from the ORDS request? It is not defined as a parameter ORDS.DEFINE_PARAMETER

    Can you please explain how I get access to the body of a custom service (POST)? You cannot just put it in a PL/SQL package - it needs to be defined somewhere and passed.

    thanks

    ReplyDelete
  2. Basically the :body is whatever the caller is put in their post calls.
    You don't need to declare, you can just use it in your plsql.

    Hope this helps!

    ReplyDelete