Invoke a REST service from PL/SQL - make an HTTP POST request using UTL_HTTP in Oracle Database 11g XE Oracle Headquarters Redwood Shores1 e1698667100526

Invoke a REST service from PL/SQL – make an HTTP POST request using UTL_HTTP in Oracle Database 11g XE

This article is small and simple. It discusses how from PL/SQL an HTTP POST request can be made to a REST service. This particular service is exposed at http://localhost:9002/cinema and it expects a POST call.

Making HTTP requests from PL/SQL is fairly simple, using the supplied package UTL_HTTP. Starting in Oracle Database 11g, some security constraints are in force around network interactions. This means that before from a specific database account a PL/SQL unit can make an HTTP call to a host and port, that account needs to be explicitly granted the privilege to do so. In this case, the user is called WC.

As DBA (for example user SYS) we have to execute the following statements to free the way for the WC account:

grant execute on utl_http to wc
grant execute on dbms_lock to wc

    acl          => 'local_sx_acl_file.xml', 
    description  => 'A test of the ACL functionality',
    principal    => 'WC',
    is_grant     => TRUE, 
    privilege    => 'connect',
    start_date   => SYSTIMESTAMP,
    end_date     => NULL);

    acl         => 'local_sx_acl_file.xml',
    host        => 'localhost', 
    lower_port  => 9002,
    upper_port  => NULL);    

Through these statements, we create an Access Control List called local_sx_acl_file.xml that is associated with database user WC. The privilege that is assigned per direct is connect. One of the specific hosts that the connect privilege applies to is configured in the last statement: localhost that may be accessed on ports 9002 and above.

After these preparations by the DBA allow the WC account to start making the call outs.

Connect as user WC and create the following PL/SQL procedure:

create or replace
procedure publish_cinema_event
( p_room_id in varchar2
, p_party_size in number
) is
  req utl_http.req;
  res utl_http.resp;
  url varchar2(4000) := 'http://localhost:9002/cinema';
  name varchar2(4000);
  buffer varchar2(4000); 
  content varchar2(4000) := '{"room":"'||p_room_id||'", "partySize":"'||p_party_Size||'"}';

  req := utl_http.begin_request(url, 'POST',' HTTP/1.1');
  utl_http.set_header(req, 'user-agent', 'mozilla/4.0'); 
  utl_http.set_header(req, 'content-type', 'application/json'); 
  utl_http.set_header(req, 'Content-Length', length(content));

  utl_http.write_text(req, content);
  res := utl_http.get_response(req);
  -- process the response from the HTTP call
      utl_http.read_line(res, buffer);
    end loop;
    when utl_http.end_of_body 
end publish_cinema_event;

This procedures publishes a JSON body in an HTTP POST request to the REST service. The payload is composed using the two input parameters passed into the procedure.

Execute this anonymous PL/SQL statement to have the procedure sent three JSON payloads to the REST service.

  publish_cinema_event('2', -4);
  publish_cinema_event('1', 4);
  publish_cinema_event('3', -1);


  1. Rahul August 7, 2018
  2. Dennis John February 15, 2018
  3. Rags January 25, 2018
  4. Sum November 22, 2017
  5. ashok verma September 9, 2017
  6. ashok verma September 9, 2017
  7. salman October 3, 2016
  8. Mohmmad Msadi April 24, 2016
    • Jibon November 7, 2017