Skip to Main Content
  • Questions
  • Need to call Restful API using Oracle PL SQL

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Karanvir.

Asked: May 23, 2016 - 1:26 am UTC

Last updated: June 12, 2018 - 1:08 am UTC

Version: Oracle Client 12.1.0.2.0

Viewed 50K+ times! This question is

You Asked

Hi,
I am new to hitting Restful APIs from Oracle.
I have this huge Xml (> 4000 characters) which I need to post to a remote restful api endpoint. Please let me know how to accomplish this.
Below is my sample code that I am playing with right now.


create or replace
procedure publish_message
(
v_messageRequest in XmlType --Input Param for Large Xml
) is
req utl_http.req;
res utl_http.resp;
url varchar2(4000) := 'my endpoint Url';
name varchar2(4000);
buffer varchar2(4000);
content XmlType := v_messageRequest;

begin
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/xml');
utl_http.set_header(req, 'Authorization', 'Basic d124553rpwYXNzd29yZA=='); --It Uses Basic Authorization
utl_http.set_header(req, 'Content-Length', length(content.getStringVal())); --Getting Length of Xml being passed

SYS.utl_http.write_raw(req, contentgetStringVal());
res := utl_http.get_response(req);
-- process the response from the HTTP call
begin
loop
utl_http.read_line(res, buffer);
dbms_output.put_line(buffer);
end loop;
utl_http.end_response(res);
exception
when utl_http.end_of_body
then
utl_http.end_response(res);
end;
end publish_message;

The issue is, when I am trying to execute this Proc:
begin
publish_message(myhugeXml);
end;
/

I get this below error:
Error report:
ORA-29273: HTTP request failed
ORA-24247: network access denied by access control list (ACL)
ORA-06512: at "SYS.UTL_HTTP", line 368
ORA-06512: at "SYS.UTL_HTTP", line 1118
ORA-06512: at "FANWMLM16.PUBLISH_MESSAGE", line 13
ORA-06512: at line 58
29273. 00000 - "HTTP request failed"
*Cause: The UTL_HTTP package failed to execute the HTTP request.
*Action: Use get_detailed_sqlerrm to check the detailed error message.
Fix the error and retry the HTTP request.

Regarding the error above: ORA-24247: network access denied by access control list (ACL)
I have given all the access to my current user using the snippets below:

begin
dbms_network_acl_admin.create_acl (
acl => 'http_permissions.xml', -- or any other name
description => 'HTTP Access',
principal => 'CurrentUser', -- the user name trying to access the network resource
is_grant => TRUE,
privilege => 'connect',
start_date => null,
end_date => null
);
end;
/
commit;

begin
DBMS_NETWORK_ACL_ADMIN.ADD_PRIVILEGE(acl => 'http_permissions.xml',
principal => 'CurrentUser',
is_grant => true,
privilege => 'connect');
end;
/
commit;

Still that doesn't resolve the error either.
Please guide me if I am doing anything wrong here.

and Connor said...

Things are a little different in 12c, so try this first, and then adapt for your situation

SQL> conn / as sysdba
Connected.

SQL> grant connect to demo identified by demo;

Grant succeeded.

SQL> grant execute on utl_http to demo;

Grant succeeded.

SQL> declare
  2    l_req   utl_http.req;
  3    l_resp  utl_http.resp;
  4  begin
  5    l_req  := utl_http.begin_request('http://www.oracle.com');
  6    l_resp := utl_http.get_response(l_req);
  7    utl_http.end_response(l_resp);
  8  end;
  9  /
declare
*
ERROR at line 1:
ORA-29273: HTTP request failed
ORA-24247: network access denied by access control list (ACL)
ORA-06512: at "SYS.UTL_HTTP", line 368
ORA-06512: at "SYS.UTL_HTTP", line 1118
ORA-06512: at line 5


SQL> conn / as sysdba
Connected.

SQL> begin
  2    dbms_network_acl_admin.append_host_ace (
  3      host       => 'www.oracle.com',
  4      lower_port => 80,
  5      upper_port => 80,
  6      ace        => xs$ace_type(privilege_list => xs$name_list('http'),
  7                                principal_name => 'demo',
  8                                principal_type => xs_acl.ptype_db));
  9  end;
 10  /

PL/SQL procedure successfully completed.

SQL> conn demo/demo
Connected.

SQL> declare
  2    l_req   utl_http.req;
  3    l_resp  utl_http.resp;
  4  begin
  5    l_req  := utl_http.begin_request('http://www.oracle.com');
  6    l_resp := utl_http.get_response(l_req);
  7    utl_http.end_response(l_resp);
  8  end;
  9  /

PL/SQL procedure successfully completed.



Rating

  (4 ratings)

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

Comments

Very helpful

Karan, May 23, 2016 - 9:50 pm UTC

Thanks a lot.
This was very helpful. I was able to resolve my issue and was able to make REST calls successfully.

Appreciate it.
Connor McDonald
May 24, 2016 - 1:15 am UTC

Glad we could help.

Not able to make a call from Stored procedure or function

Deepak, March 29, 2017 - 4:56 pm UTC

Hi,

I am able to make a call directly from console (oracle sql developer), but when i take the same copy and create a new stored procedure and make call to SP, I always see "network access denied by access control list(ACL)"
need help.


SET SERVEROUTPUT ON;


declare
var_url number;
req utl_http.req;
res utl_http.resp;
url varchar2(4000);
var_name varchar2(4000);
p_json  varchar2(4000);
buffer varchar2(4000); 
content varchar2(4000);
begin


p_json :='Test';

url:= 'http://loclhost:81/';
content := '{"message":"'|| p_json||'"}';
dbms_output.put_line(content);
dbms_output.put_line(url);
 req := utl_http.begin_request(url, 'PUT',' 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);
  
   begin
    loop
      utl_http.read_line(res, buffer);
      dbms_output.put_line(buffer);
    end loop;
    utl_http.end_response(res);
  exception
    when utl_http.end_of_body 
    then
      utl_http.end_response(res);
  end;
end;



VIA SYS window


grant execute on utl_http to ITAS;
grant execute on dbms_lock to ITAS;
 
BEGIN
  DBMS_NETWORK_ACL_ADMIN.create_acl (
    acl          => 'test-dev02_sx_acl_file.xml', 
    description  => 'Publish event message',
    principal    => 'ITAS',
    is_grant     => TRUE, 
    privilege    => 'connect'
    );
 

DBMS_NETWORK_ACL_ADMIN.assign_acl (
    acl         => 'test-dev02_sx_acl_file.xml',
    host        => 'http://localhost', 
    lower_port  => 80,
    upper_port  => 82);    
    
  DBMS_NETWORK_ACL_ADMIN.ADD_PRIVILEGE(acl       => 'hivedome-dev02_sx_acl_file.xml',
                                       principal => 'ITAS',
                                       is_grant  => true,
                                       privilege => 'resolve');
                                       
  
end; 


thanks
Deepak

Connor McDonald
April 13, 2017 - 2:26 am UTC

What happens if you do

set role none

and then run it from an anonymous block ?


Does not work in Oracle RDS 12.c

Miomir, November 01, 2017 - 8:24 pm UTC

Hello, the attached steps to activate utl_http seem very useful, but even though they execute till the last one, they don't give necessary permissions to the Oracle RDS 12c user (AWS), as tested on Nov 1, 2017.

In the last step the assigned user running sample script to connect to www.oracle.com receives error message:

ORA-29273: HTTP request failed
ORA-24247: network access denied by access control list (ACL)
ORA-06512: at "SYS.UTL_HTTP", line 1258
ORA-06512: at line 6
29273. 00000 - "HTTP request failed"
*Cause: The UTL_HTTP package failed to execute the HTTP request.
Connor McDonald
November 03, 2017 - 7:30 am UTC

That's not a limitation of the database but of the layer that Amazon have possibly put around it.

You'd need to speak to them.

Need help

A reader, June 11, 2018 - 11:27 am UTC

Hi,

I am able to make a call directly from console (oracle sql developer), but when i take the same copy and create a new stored procedure or function and make call to SP, I always see "network access denied by access control list(ACL)
Connor McDonald
June 12, 2018 - 1:08 am UTC

We need a reproducible test case

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library