PLSQL PROCEDURE TO CALL A REST API POST METHOD

 PLSQL PROCEDURE TO CALL A REST API POST METHOD

create or replace procedure XXXCC_XXX_XXX_PROC(oredrno number) as
-- varibles for running REST API
  l_http_request  UTL_HTTP.req;
  l_http_response UTL_HTTP.resp;
  l_response_text CLOB;
  l_payload       CLOB;
--below 3 are non mandatroy varibales only to store data from table to paas in payload
  NUM varchar2(200);
  RNAME varchar2(200);
BEGIN

--sql query only use if you are passing values from tables into api
SELECT a,b,into num,rname FROM dual where ord = oredrno;

-- Create the payload
l_payload := '{
    "method": "add",
    "data": {
      "recipient_details": {
        "recp_mobile": "'||NUM||'",
        "recp_name": "'||RNAME||'"
      },
  }';

  -- Initialize the HTTP request              URL                  METHOD
  l_http_request := UTL_HTTP.begin_request('http://x.x/xxx_xxxx', 'POST');
 
  --use this if your authentication type is basic here i have used bearer type
  --UTL_HTTP.SET_AUTHENTICATION(r => l_http_request, username => 'IDAS',password => 'id34');
 
  -- Set the headers
  UTL_HTTP.set_header(l_http_request, 'Content-Type', 'application/json');
  UTL_HTTP.set_header(l_http_request, 'Authorization', 'Bearer dfjnsfjkf439439055555555555555555502934pk');
  UTL_HTTP.set_header(l_http_request, 'Content-Length', LENGTH(l_payload));

  -- Write the payload to the request
  UTL_HTTP.write_text(l_http_request, l_payload);

  -- Get the response
  l_http_response := UTL_HTTP.get_response(l_http_request);

  -- Read the response into a variable
  DBMS_LOB.createtemporary(l_response_text, TRUE);
  UTL_HTTP.read_text(l_http_response, l_response_text);
  UTL_HTTP.end_response(l_http_response);

  -- Process the response
  DBMS_OUTPUT.put_line('Response: ' || DBMS_LOB.substr(l_response_text, 32767, 1));

EXCEPTION
  WHEN UTL_HTTP.TOO_MANY_REQUESTS THEN
    -- Handle too many requests error
    DBMS_OUTPUT.put_line('Too many requests');
END;

Comments

Popular posts from this blog

opaque schema xsd (standard file used in OIC)

SQL,PLSQL interview practice and DSA patterns

QR code Generate From Oracle PLSQL