ferenit
ferenit

Reputation: 51

Sending CLOB data of length 32000 in utl_http.req throws ORA:06052 POST method

I am trying to send an XML as a URL parameter from PL/SQL. But when I try to send it the error "ORA:06052" occurs. Below is the PL/SQL code

      CREATE OR REPLACE FUNCTION EMASDB.ESIGN(TY IN VARCHAR2,DATA1 IN CLOB,DATA2 IN CLOB) RETURN clob
IS
XML CLOB;
v_data_post CLOB;
resp utl_http.resp;
req  utl_http.req;
v_txt CLOB;
BEGIN
IF TY='REGISTER' THEN
XML:='<register><uniqueid>'||DATA2||'</uniqueid><DATA1>'||DATA1||'</DATA1><userEnable>true</userEnable><originalContent>'||DATA2||'</originalContent></register>';
ELSIF TY='AUTHENTICATE' THEN
XML :='<AuthenticateDet><uniqueId>'||DATA2||'</uniqueId><DATA1>'||DATA1||'</DATA1><originalContent>'||DATA2||'</originalContent><referenceNo></referenceNo></AuthenticateDet>';
ELSE
XML :='<verifyDet><dataType>pkcs7</dataType><DATA1>'||DATA1||'</DATA1><originalContent>'||DATA2||'</originalContent><responseFormat>plain</responseFormat></verifyDet>';
DBMS_OUTPUT.PUT_LINE('A');
END IF;
req  := UTL_HTTP.begin_request ('url','POST','HTTP/1.1');
utl_http.set_header(req, 'Content-Type', 'application/x-www-form-urlencoded');
utl_http.set_header(req, 'Content-Length', length(XML));
v_data_post :='xml='||XML;
/*utl_http.write_text(req, v_data_post);
resp := UTL_HTTP.get_response(req);
utl_http.read_text(resp,v_txt);
utl_http.end_response(resp);
RETURN v_txt;*/
RETURN 'done';
END;
/

Upvotes: 2

Views: 18202

Answers (4)

Edemir Araujo
Edemir Araujo

Reputation: 11

for those who still need it, you can try this way :

begin

declare
    v_req             utl_http.req;
    v_res             utl_http.resp;
    vn_tam_clob       PLS_INTEGER;
    vn_tam_buffer     PLS_INTEGER;
    vn_pos_inicial    PLS_INTEGER;
    v_buffer          varchar2(32000); 
    v_body            clob := va_content_clob; -- Your JSON (XML)
    
begin
    -- Set connection.

    vn_tam_clob  := DBMS_LOB.GETLENGTH(va_content_clob);

    v_req := utl_http.begin_request('http://your.api/operation', 'POST');
    utl_http.set_authentication(v_req, 'your_username','your_password');
    utl_http.set_header(v_req, 'content-type', 'application/json'); 
    utl_http.set_header(v_req, 'Content-Length', vn_tam_clob);
    
    -- Set error to exception

    utl_http.set_response_error_check(true);

    -- Invoke REST API.
      
      vn_pos_inicial := 1;
      vn_tam_buffer  := 32000;

      while vn_pos_inicial <= vn_tam_clob -- least( vn_tam_clob, vn_tam_buffer ) ) 
       loop
        utl_http.write_text( v_req, dbms_lob.substr( v_body, vn_tam_buffer, vn_pos_inicial ) );
        vn_pos_inicial := vn_pos_inicial + vn_tam_buffer ;
      end loop;

    -- Get response para retorno OK ou analise do problema.
    
    v_res := utl_http.get_response(v_req);

-- if you don't set : utl_http.set_response_error_check(true); -- you can get the return structure

    -- IF (v_res.status_code = 202) THEN
    --    dbms_output.put_line('v_response is okay');
    -- ELSE
    --    dbms_output.put_line('v_resPONSE CODE IS '||v_res.status_code);
    -- 
    --  begin
    --      loop
    --          utl_http.read_line(v_res, v_buffer);
    -- 
    --          -- display or log in table  v_buffer;
    -- 
    -- 
    --      end loop;
    --      utl_http.end_response(v_res);
    --  exception
    --      when utl_http.end_of_body then
    --          utl_http.end_response(v_res);
    --  end;
    -- 
    -- END IF;

END;

EXCEPTION

WHEN OTHERS THEN utl_http.end_response(v_res); RAISE;

END;

Upvotes: 1

brunomaso1
brunomaso1

Reputation: 81

For all of you who are struggling with similar issues, this solution also resolved the following Oracle errors:

  • Error: ORA-29273: fallo de la solicitud HTTP
  • ORA-12547: TNS:contacto perdido

Upvotes: 0

hamza ostonov
hamza ostonov

Reputation: 1

You should NEVER send Transfer-Encoding: chunked , alongside Content-Length , its either the one of the other. – Jelman Jan 21, 2020 at 13:54

helped me!

Upvotes: 0

Brian McGinity
Brian McGinity

Reputation: 5935

When you need write (and read) more than 32k in utl_http, it done a bit differently.

For example:

l_data := 'fill with some sample piece of text';
http_req := utl_http.begin_request(url => 'http://example.com', method => 'POST');
utl_http.set_header (http_req, 'Content-Length', length(l_data));
utl_http.set_header (http_req, 'Content-Type', 'text/xml;charset=UTF-8');
utl_http.set_header (http_req, 'Transfer-Encoding', 'chunked');

loop
  l_chunkData := null;
  l_chunkData := substr(l_data, l_chunkStart, l_chunkLength);
  utl_http.write_text(http_req, l_chunkData);
  if (length(l_chunkData) < l_chunkLength) then exit; end if;
  l_chunkStart := l_chunkStart + l_chunkLength;
end loop;

See: http://www.kurzhals.info/2012/03/using-chunked-transfer-with-plsql-utl_http-write_text.html

And for a clob example see: http://blog.optiosys.com/?p=246

Upvotes: 4

Related Questions