How to hit a http URL from PL/SQL procedure? - oracle

Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
I am creating an Oracle job where I need to hit a procedure for every 30 minutes,
Inside the procedure, I want to hit a HTTP URL so behind that a java program will execute.
Approach :
declare
req UTL_HTTP.REQ;
BEGIN
req := UTL_HTTP.BEGIN_REQUEST('http://dev.xxx.com/yyy/zzz/aaa/triggerJob');
dbms_output.put_line('hitting');
EXCEPTION
WHEN UTL_HTTP.END_OF_BODY THEN
dbms_output.put_line('exception');
END;
DBMS OUTPUT is hitting
-- But it is not hitting actually!
Approach 2
declare
req UTL_HTTP.REQ;
resp UTL_HTTP.RESP;
BEGIN
req := UTL_HTTP.BEGIN_REQUEST('http://dev.xxx.com/yyy/zzz/aaa/triggerJob');
UTL_HTTP.SET_HEADER(req, 'User-Agent', 'Mozilla/4.0');
resp := UTL_HTTP.GET_RESPONSE(req);
UTL_HTTP.END_RESPONSE(resp);
dbms_output.put_line('hitting');
EXCEPTION
WHEN UTL_HTTP.END_OF_BODY THEN
dbms_output.put_line('exception');
END;
With this, I am getting below errors while executing.

Consider adding a get_response to actually perform the request; otherwise its only prepared ;) ; another good practice is to set the http headers...
declare
req UTL_HTTP.REQ;
BEGIN
req := UTL_HTTP.BEGIN_REQUEST('http://dev.xxx.com/yyy/zzz/aaa/triggerJob');
UTL_HTTP.SET_HEADER(req, 'User-Agent', 'Mozilla/4.0');
UTL_HTTP.GET_RESPONSE(req);
dbms_output.put_line('hitting');
EXCEPTION
WHEN UTL_HTTP.END_OF_BODY THEN
dbms_output.put_line('exception');
END;
more in Oracle documentation

Related

How to fix the error of "HTTP request failed" while calling an API in a oracle procedure?

I am trying to call an API from a procedure in oracle , for a demo purpose I tried calling a web page but its giving me error :
*Cause: The UTL_HTTP package failed to execute the HTTP request.
Set serveroutput on ;
DECLARE
req UTL_HTTP.REQ;
resp UTL_HTTP.RESP;
value VARCHAR2(1024);
BEGIN
req := UTL_HTTP.BEGIN_REQUEST('http://www.nyquest.com');
resp := UTL_HTTP.GET_RESPONSE(req);
LOOP
UTL_HTTP.READ_LINE(resp, value, TRUE);
dbms_output.put_line(value);
END LOOP;
UTL_HTTP.END_RESPONSE(resp);
EXCEPTION
WHEN UTL_HTTP.END_OF_BODY THEN
UTL_HTTP.END_RESPONSE(resp);
END;
Please guide me in fixing this error .
Most likely you didnt created ACL's.
Your database needs it before it is going to accept connections from outside.
You can read about it here:
Oracle Access Control List

"ORA-29024: Certificate validation failure"

I am trying to consume Rest API in oracle procedure. I have created ACL entry as well as Added the SSL certificate to my oracle wallet manager. but when i am trying to execute the procedure i am getting the error as "ORA-29024: Certificate validation failure". Following is the code for my oracle procedure.
create or replace procedure TABADUL_TAS_AUTHENTICATION
is
req utl_http.req;
res utl_http.resp;
value VARCHAR2(1024);
value1 VARCHAR2(1024);
url varchar2(4000) := 'https://tapis.fasah.sa/tabadul/fasahqa/authorization/token';
name varchar2(4000);
buffer varchar2(4000);
content varchar2(4000) := '{"username":"ijsnj001","password":"P#ssw0rd"}';
begin
UTL_HTTP.set_wallet('file:d:\tabadul', 'tas123456');
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;charset=utf-8');
utl_http.set_header(req,'X-IBM-Client-Id','00a2f36e933e2bb9edc76faaf26659eb');
utl_http.set_header(req,'X-IBM-Client-Secret','7c2829bd6b287b072ee269c9ad8f5ead');
utl_http.set_header(req,'Accept-Language','en');
utl_http.set_header(req,'Accept','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
DBMS_OUTPUT.PUT_LINE('HTTP response status code: ' || res.status_code);
--DBMS_OUTPUT.PUT_LINE('HTTP response reason phrase: ' || res.reason_phrase);
FOR i IN 1..UTL_HTTP.GET_HEADER_COUNT(res) LOOP
UTL_HTTP.GET_HEADER(res, i, name, value1);
--DBMS_OUTPUT.PUT_LINE(name || ': ' || value1);
END LOOP;
--dbms_output.put_line(content);
--dbms_output.put_line(utl_http.resp);
begin
loop
--dbms_output.put_line('A');
utl_http.read_line(res, value, true);
--dbms_output.put_line(length(value));
dbms_output.put_line(value);
--INSERT INTO A VALUES (VALUE);
--COMMIT;
end loop;
utl_http.end_response(res);
exception
when utl_http.end_of_body
then
--dbms_output.put_line(SQLCODE||','||SQLERRM);
utl_http.end_response(res);
when others then
--dbms_output.put_line(SQLCODE||','||SQLERRM);
utl_http.end_response(res);
end;
end TABADUL_TAS_AUTHENTICATION;
This issue has been resolved.I am writing the solution here, it might be helpful for others.
So instead of creating wallet for the actual website certificate, need to create wallet only for chain of certificate.

REST calls to a self-signed,not certified website using Oracle APEX

I'm trying to post some data to a website using APEX, by executing a PL/SQL code.
The problem is, the website's certificate is self-signed, so it's not properly certified, and I'm getting the below error:
ORA-29273: HTTP request failed ORA-06512: at "SYS.UTL_HTTP", line 1130
ORA-28860: Fatal SSL error
I'm using the below code:
procedure publish_error_tickets
is
req utl_http.req;
res utl_http.resp;
url varchar2(4000) := 'https://some_url';
name varchar2(4000);
buffer varchar2(4000);
content varchar2(4000) :=
'{
"issue": {
"project_id": 1,
"subject": "Example",
"priority_id": 4
}
}'; 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/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;
My question is, is it possible to somehow ignore the certificate validation process for the http message? There must be a way to reach a website without the need to buy a validated cert for the site.
Thanks in advance,
Tamas
For anyone having the same problem, I got it working with the solution Jeffrey Kemp posted( http://blog.rhjmartens.nl/2015/07/making-https-webservice-requests-from.html ), with a reverse proxy, it works like a charm!

Oracle database DNS resolution for UTL_HTTP taking time

I am having problem in making UTL_HTTP call from Oracle DB. When I am using IP based URL results are coming fast, but when using DNS based URL 1 out of 10 request is taking extra 5 sec for calling URL.
Below is the sample code
declare
l_url VARCHAR2(500) := 'SOMEURL';
l_http_request UTL_HTTP.req;
l_http_response UTL_HTTP.resp;
BEGIN
DBMS_output.put_line(systimestamp);
FOR i IN 1..100 LOOP
-- Make a HTTP request and get the response.
l_http_request := UTL_HTTP.begin_request(l_url);
l_http_response := UTL_HTTP.get_response(l_http_request);
UTL_HTTP.end_response(l_http_response);
--DBMS_output.put_line (l_http_response.status_code);
END LOOP;
DBMS_output.put_line(systimestamp);
END;
/

ORA-28759: failure to open file

I have a problem.
When I use UTL_HTTP.SET_WALLET Oracle says me next errors:
ORA-29273: HTTP request failed
ORA-06512: at "SYS.UTL_HTTP", line 1130
ORA-28759: failure to open file
Previously, I use Oracle Wallet Manager for setup the Sertificate from website "https://focus-api.kontur.ru/api3/". Cannot understand, what am I doing wrong.
DECLARE
HTTP_REQ UTL_HTTP.REQ;
HTTP_RESP UTL_HTTP.RESP;
URL_TEXT VARCHAR2(32767);
BEGIN
DBMS_OUTPUT.ENABLE(1000000);
UTL_HTTP.SET_WALLET('file:d:\oracle\product\11.2.0\dbhome_1\NETWORK\wallet', 'password');
HTTP_REQ := UTL_HTTP.BEGIN_REQUEST('https://focus-api.kontur.ru/api3/req?inn=7714037390');
UTL_HTTP.SET_HEADER(HTTP_REQ, 'User-Agent', 'Mozilla/4.0');
HTTP_RESP := UTL_HTTP.GET_RESPONSE(HTTP_REQ);
LOOP
BEGIN
URL_TEXT := null;
UTL_HTTP.READ_LINE(HTTP_RESP, URL_TEXT, TRUE);
DBMS_OUTPUT.PUT_LINE(URL_TEXT);
EXCEPTION
WHEN OTHERS THEN EXIT;
END;
END LOOP;
UTL_HTTP.END_RESPONSE(HTTP_RESP);
END;
/

Resources