Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, HARI PRASAD.

Asked: December 16, 2020 - 3:04 am UTC

Last updated: December 16, 2020 - 4:15 pm UTC

Version: 11GR2

Viewed 1000+ times

You Asked

Hi Tom,

We are preparing request in database as JSON.

v_http_request CLOB;
v_req_body CLOB;

 v_http_request := '{"url":"' || www.oracle.com|| 
                       '","message_id":"' || 1234|| 
                       '","header":"' ||Content-Type#text/xml; charset=UTF-8;Content-Length#872 || 
                       '","body_char_set":"' || UTF-8 ||
                       '","timeout":"' || 60 || 
                       '","proxy_url":"' || null||
                       '","http_method":"' || POST ||
                       '","http_version":"' || 1.1 ||
                       '","http_req_body":"' || v_req_body|| 
                      '"}';


And req_body contains XML message.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header/>
  <soapenv:Body>
    .....
  </soapenv:Body>
</soapenv:Envelope>


The problem we are facing is req body is XML and it has "" in between and JSON parser is giving issues.
And there is not method we see that to convert or apply encode and decode on CLOB column.

And q'[req_body] -- quotation is working only for 32767 characters.

Can you please advise how to handle this scenario.

Thank You

and Chris said...

You could try:

- Converting problematic characters to their HTML code (e.g. double-quotes => &quot;) OR
- base64 encoding the XML document

e.g.
set define off
declare
  jdoc varchar2(4000);
  xdoc xmltype;
begin
  xdoc := xmltype ( q'!<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header/>
  <soapenv:Body>
  </soapenv:Body>
</soapenv:Envelope>!' );

  jdoc :=
    ' { "url " : "www.oracle.com" , "message_id" : 1234, "xml":"' || 
    replace (
      replace ( 
        replace ( 
          replace (
            xdoc.getStringVal(), 
            '"', '&quot;' 
          ), '/', '&#x2F;'
        ), '<', '&lt;'
      ), '>', '&gt;'
    ) || '" }';
  
  dbms_output.put_line ( jdoc );

  jdoc := 
    ' { "url " : "www.oracle.com" , "message_id" : 1234, "xml":"' || 
    utl_raw.cast_to_varchar2 (
      utl_encode.base64_encode ( 
        utl_raw.cast_to_raw ( 
          xdoc.getStringVal() 
        )
      )
    ) || '", "encoding" : "base64" }';
    
  dbms_output.put_line ( jdoc );
end;
/

{ "url " : "www.oracle.com" , "message_id" : 1234, "xml":"&lt;soapenv:Envelope xmlns:soapenv=&quot;http:&#x2F;&#x2F;schemas.xmlsoap.org&#x2F;soap&#x2F;envelope&#x2F;&quot;&gt;
  &lt;soapenv:Header&#x2F;&gt;
  &lt;soapenv:Body&gt;
  &lt;&#x2F;soapenv:Body&gt;
&lt;&#x2F;soapenv:Envelope&gt;" }
 { "url " : "www.oracle.com" , "message_id" : 1234, "xml":"PHNvYXBlbnY6RW52ZWxvcGUgeG1sbnM6c29hcGVudj0iaHR0cDovL3NjaGVtYXMu
eG1sc29hcC5vcmcvc29hcC9lbnZlbG9wZS8iPgogIDxzb2FwZW52OkhlYWRlci8+
CiAgPHNvYXBlbnY6Qm9keT4KICA8L3NvYXBlbnY6Qm9keT4KPC9zb2FwZW52OkVu
dmVsb3BlPg==", "encoding" : "base64" }


Is this answer out of date? If it is, please let us know via a Comment

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.