Oracle database DNS resolution for UTL_HTTP taking time - oracle

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;
/

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.

Consuming rest API from Oracle PLSQL is returning ??? for arabic characters

I am trying to call a restful API from my Oracle procedure.
First, the API method is of type get and not post, so parameters are sent through header. the main purpose of the API is to send the received message as SMS to some providers and sometimes they are in Arabic format; We realized that Arabic received SMS are incomprehensible;
So I created a test procedure that takes a message and sends it to a test API method that returns the same message as response.
The API call succeeded but the response, only when arabic format is used, looks like ����. What should be added to my procedure so messages can be readable? I have tried to use escape for the message and to set the header format as you can see in below template, but unfortunately nothing succeeded:
PROCEDURE TEST(lang VARCHAR2,
message VARCHAR2,
P_RESPONSE OUT VARCHAR2) AS
v_request UTL_HTTP.req;
v_response UTL_HTTP.resp;
v_text VARCHAR2(1024);
v_url VARCHAR2(1024);
v_message VARCHAR2(1024);
l_webservice_link VARCHAR2(128);
BEGIN
BEGIN
P_RESPONSE := '';
v_message := utl_url.escape(message);
--v_message :=utl_url.escape(message,false,'UTF-8');
--v_message :=utl_url.escape(message,false,'windows-1256');
--v_message :=utl_url.escape(message,false,'AL32UTF8');
--v_message :=utl_url.escape(message,false,'AR8MSWIN1256');
l_webservice_link := GET_PARAM('REST_API_URL');
v_url := l_webservice_link ||
'Mytest?strMessage=' || v_message||
'&strLang=' || lang;
v_request := UTL_HTTP.begin_request(v_url);
--UTL_HTTP.set_header(v_request, 'Content-Type', 'charset=UTF-8');
--UTL_HTTP.set_header(v_request, 'Content-Type', 'windows-1256');
v_response := UTL_HTTP.get_response(v_request);
LOOP
BEGIN
UTL_HTTP.read_text(v_response, v_text);
DBMS_OUTPUT.put_line(v_text);
EXCEPTION
WHEN UTL_HTTP.end_of_body THEN
NULL;
END;
EXIT WHEN v_text IS NULL;
END LOOP;
UTL_HTTP.end_response(v_response);
IF v_response.status_code <> 200 THEN
P_RESPONSE := v_response.reason_phrase;
END IF;
EXCEPTION
WHEN OTHERS THEN
P_RESPONSE := 'An error has occured: ' || SQLERRM;
END;
END TEST;
Any help is more than appreciated.
Try to insert this code:
Charset VARCHAR2(20);
BEGIN
SELECT UTL_I18N.MAP_CHARSET(VALUE)
INTO Charset
FROM nls_database_parameters
WHERE parameter = 'NLS_CHARACTERSET';
UTL_HTTP.set_header(v_request, 'Content-Type', 'text/html; charset='||Charset);
I am not familiar with REST, I don't know wether text/html; is required and correct.
Update
I just see your database character set it AR8ASMO8X which does not have any IANA name (at least not according to Oracle UTL_I18N.MAP_CHARSET)
In this case try
UTL_HTTP.set_header(v_request, 'Content-Type', 'text/html; charset=UTF-8');
UTL_HTTP.begin_request(CONVERT(v_url,'AL32UTF8'));
Most likely the server returns response in UTF-8 - would be the most common one, otherwise check the header of the response.
Then try this:
UTL_HTTP.SET_BODY_CHARSET(v_response, 'AL32UTF8');
Apart from all above you may have also a display issue, i.e. inside Oracle everything would be fine, just your client is not able to display the characters properly, see OdbcConnection returning Chinese Characters as "?"

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

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

Accessing HTTP Header Info

I'm using Oracle 11g, together with F5 BIG-IP network and Glassfish app server and was wondering how, using pl/sql, access HTTP Header GET information, which should also include LDAP info from the F5?
Specifically, maybe this part will help:
--
Retrieving HTTP Response Headers
SET SERVEROUTPUT ON SIZE 40000
DECLARE
req UTL_HTTP.REQ;
resp UTL_HTTP.RESP;
name VARCHAR2(256);
value VARCHAR2(1024);
BEGIN
UTL_HTTP.SET_PROXY('proxy.my-company.com', 'corp.my-company.com');
req := UTL_HTTP.BEGIN_REQUEST('http://www-hr.corp.my-company.com');
UTL_HTTP.SET_HEADER(req, 'User-Agent', 'Mozilla/4.0');
resp := UTL_HTTP.GET_RESPONSE(req);
DBMS_OUTPUT.PUT_LINE('HTTP response status code: ' || resp.status_code);
DBMS_OUTPUT.PUT_LINE('HTTP response reason phrase: ' || resp.reason_phrase);
FOR i IN 1..UTL_HTTP.GET_HEADER_COUNT(resp) LOOP
UTL_HTTP.GET_HEADER(resp, i, name, value);
DBMS_OUTPUT.PUT_LINE(name || ': ' || value);
END LOOP;
UTL_HTTP.END_RESPONSE(resp);
END;
Have you looked at the UTL_HTTP supplied package documentation? Not sure it has what you're looking for, but I'd start there.

Resources