Click to add to Favorites How to POST and RECEIVE a response as a CLOB using UTL_HTTP? (Doc ID 1375312.1) To BottomTo Bottom
In this Document
Goal
Solution
APPLIES TO:
PL/SQL - Version 11.2.0.3 and later
Information in this document applies to any platform.
GOAL
How to POST and RECEIVE a response as a CLOB using UTL_HTTP?
SOLUTION
create or replace function PostRecClob(url varchar2, request clob) return clob as
req utl_http.req;
resp utl_http.resp;
length binary_integer;
response clob;
buffer varchar2(32767);
amount pls_integer := 32767;
offset pls_integer := 1;
begin
req := utl_http.begin_request(url, 'POST', 'HTTP/1.1');
utl_http.set_header(req, 'Content-Type', 'text/xml');
utl_http.set_header(req, 'Transfer-Encoding', 'chunked');
length := dbms_lob.getlength(request);
while(offset < length) loop
dbms_lob.read(request, amount, offset, buffer);
utl_http.write_text(req, buffer);
offset := offset + amount;
end loop;
resp := utl_http.get_response(req);
-- Code to read the response in 32k chunks
dbms_lob.createtemporary(response, false);
begin
loop
utl_http.read_text(resp, buffer);
dbms_lob.writeappend(response, dbms_lob.getlength(buffer), buffer);
end loop;
utl_http.end_response(resp);
exception
when utl_http.end_of_body then
utl_http.end_response(resp);
end;
return response;
end;
/
Sample Code to call or execute the function PostRecClob:
set serverout on
declare
request clob;
xmlrequest xmltype;
response clob;
xmlresponse xmltype;
data clob;
begin
for i in 1..1000 loop
request := request||'ABCDEFGH';
end loop;
response := PostRecClob('http://Your_URL/YourServlet', request);
dbms_output.put_line(response);
end;
/