UTL_HTTP¶
The UTL_HTTP package provides a way to use the HTTP or HTTPS protocol
to retrieve information found at an URL. Advanced Server supports the following functions and procedures:
Function/Procedure |
Return Type |
Description |
|
|
Initiates a new HTTP request. |
|
n/a |
Ends an HTTP request before allowing it to complete. |
|
n/a |
Ends the HTTP response. |
|
n/a |
Ends package body |
|
|
Returns the default character set of the body of future HTTP requests. |
|
n/a |
Returns the default character set of the body of future HTTP requests. |
|
n/a |
Current setting for the maximum number of redirections allowed. |
|
n/a |
Returns the nth header of the HTTP response. |
|
n/a |
Returns the HTTP response header for the specified name. |
|
|
Returns the number of HTTP response headers. |
|
|
Returns the HTTP response. |
|
n/a |
Returns whether or not response error check is set. |
|
n/a |
Returns the transfer timeout setting for HTTP requests. |
|
n/a |
Returns the HTTP response body in text form until the end of line. |
|
n/a |
Returns the HTTP response body in binary form for a specified number of bytes. |
|
n/a |
Returns the HTTP response body in text form for a specified number of characters. |
|
|
Returns the content of a web page. |
|
|
Returns a table of 2000-byte segments retrieved from an URL. |
|
n/a |
Sets the default character set of the body of future HTTP requests. |
|
n/a |
Sets the maximum number of times to follow the redirect instruction. |
|
n/a |
Sets the maximum number of times to follow the redirect instruction for an individual request. |
|
n/a |
Sets the HTTP request header. |
|
n/a |
Determines whether or not HTTP 4xx and 5xx status codes are to be treated as errors. |
|
n/a |
Sets the default, transfer timeout value for HTTP requests. |
|
n/a |
Sets the transfer timeout value for an individual HTTP request. |
|
n/a |
Writes CRLF terminated data to the HTTP request body in TEXT form. |
|
n/a |
Writes data to the HTTP request body in BINARY form. |
|
n/a |
Writes data to the HTTP request body in TEXT form. |
Advanced Server’s implementation of UTL_HTTP is a partial
implementation when compared to Oracle’s version. Only those functions
and procedures listed in the table above are supported.
Note
In Advanced Server, an HTTP 4xx or HTTP 5xx response produces a database error; in Oracle, this is configurable but FALSE by default.
In Advanced Server, the UTL_HTTP text interfaces expect the downloaded
data to be in the database encoding. All currently-available interfaces
are text interfaces. In Oracle, the encoding is detected from HTTP
headers; in the absence of the header, the default is configurable and
defaults to ISO-8859-1.
Advanced Server ignores all cookies it receives.
The UTL_HTTP exceptions that can be raised in Oracle are not recognized
by Advanced Server. In addition, the error codes returned by Advanced
Server are not the same as those returned by Oracle.
UTL_HTTP Exception Codes
If a call to a UTL_HTTP procedure or function raises an exception, you
can use the condition name to catch the exception. The UTL_HTTP package
reports the following exception codes compatible with Oracle databases:
Exception Code |
Condition Name |
Description |
Raised Where |
|---|---|---|---|
-29266 |
|
The end of HTTP response body is reached |
|
To use the UTL_HTTP.END_OF_BODY exception, first you need to run the utl_http_public.sql file from the contrib/utl_http directory of your installation directory.
There are various public constants available with UTL_HTTP. These are
listed in the following tables.
The following table contains UTL_HTTP public constants defining HTTP
versions and port assignments.
HTTP VERSIONS |
|
|---|---|
HTTP_VERSION_1_0 |
CONSTANT VARCHAR2(64) := ‘HTTP/1.0’; |
HTTP_VERSION_1_1 |
CONSTANT VARCHAR2(64) := ‘HTTP/1.1’; |
STANDARD PORT ASSIGNMENTS |
|
DEFAULT_HTTP_PORT |
CONSTANT INTEGER := 80; |
DEFAULT_HTTPS_PORT |
CONSTANT INTEGER := 443; |
The following table contains UTL_HTTP public status code constants.
1XX INFORMATIONAL |
|
|---|---|
HTTP_CONTINUE |
CONSTANT INTEGER := 100; |
HTTP_SWITCHING_PROTOCOLS |
CONSTANT INTEGER := 101; |
HTTP_PROCESSING |
CONSTANT INTEGER := 102; |
2XX SUCCESS |
|
HTTP_OK |
CONSTANT INTEGER := 200; |
HTTP_CREATED |
CONSTANT INTEGER := 201; |
HTTP_ACCEPTED |
CONSTANT INTEGER := 202; |
HTTP_NON_AUTHORITATIVE_INFO |
CONSTANT INTEGER := 203; |
HTTP_NO_CONTENT |
CONSTANT INTEGER := 204; |
HTTP_RESET_CONTENT |
CONSTANT INTEGER := 205; |
HTTP_PARTIAL_CONTENT |
CONSTANT INTEGER := 206; |
HTTP_MULTI_STATUS |
CONSTANT INTEGER := 207; |
HTTP_ALREADY_REPORTED |
CONSTANT INTEGER := 208; |
HTTP_IM_USED |
CONSTANT INTEGER := 226; |
3XX REDIRECTION |
|
HTTP_MULTIPLE_CHOICES |
CONSTANT INTEGER := 300; |
HTTP_MOVED_PERMANENTLY |
CONSTANT INTEGER := 301; |
HTTP_FOUND |
CONSTANT INTEGER := 302; |
HTTP_SEE_OTHER |
CONSTANT INTEGER := 303; |
HTTP_NOT_MODIFIED |
CONSTANT INTEGER := 304; |
HTTP_USE_PROXY |
CONSTANT INTEGER := 305; |
HTTP_SWITCH_PROXY |
CONSTANT INTEGER := 306; |
HTTP_TEMPORARY_REDIRECT |
CONSTANT INTEGER := 307; |
HTTP_PERMANENT_REDIRECT |
CONSTANT INTEGER := 308; |
4XX CLIENT ERROR |
|
|---|---|
HTTP_BAD_REQUEST |
CONSTANT INTEGER := 400; |
HTTP_UNAUTHORIZED |
CONSTANT INTEGER := 401; |
HTTP_PAYMENT_REQUIRED |
CONSTANT INTEGER := 402; |
HTTP_FORBIDDEN |
CONSTANT INTEGER := 403; |
HTTP_NOT_FOUND |
CONSTANT INTEGER := 404; |
HTTP_METHOD_NOT_ALLOWED |
CONSTANT INTEGER := 405; |
HTTP_NOT_ACCEPTABLE |
CONSTANT INTEGER := 406; |
HTTP_PROXY_AUTH_REQUIRED |
CONSTANT INTEGER := 407; |
HTTP_REQUEST_TIME_OUT |
CONSTANT INTEGER := 408; |
HTTP_CONFLICT |
CONSTANT INTEGER := 409; |
HTTP_GONE |
CONSTANT INTEGER := 410; |
HTTP_LENGTH_REQUIRED |
CONSTANT INTEGER := 411; |
HTTP_PRECONDITION_FAILED |
CONSTANT INTEGER := 412; |
HTTP_REQUEST_ENTITY_TOO_LARGE |
CONSTANT INTEGER := 413; |
HTTP_REQUEST_URI_TOO_LARGE |
CONSTANT INTEGER := 414; |
HTTP_UNSUPPORTED_MEDIA_TYPE |
CONSTANT INTEGER := 415; |
HTTP_REQ_RANGE_NOT_SATISFIABLE |
CONSTANT INTEGER := 416; |
HTTP_EXPECTATION_FAILED |
CONSTANT INTEGER := 417; |
HTTP_I_AM_A_TEAPOT |
CONSTANT INTEGER := 418; |
HTTP_AUTHENTICATION_TIME_OUT |
CONSTANT INTEGER := 419; |
HTTP_ENHANCE_YOUR_CALM |
CONSTANT INTEGER := 420; |
HTTP_UNPROCESSABLE_ENTITY |
CONSTANT INTEGER := 422; |
HTTP_LOCKED |
CONSTANT INTEGER := 423; |
HTTP_FAILED_DEPENDENCY |
CONSTANT INTEGER := 424; |
HTTP_UNORDERED_COLLECTION |
CONSTANT INTEGER := 425; |
HTTP_UPGRADE_REQUIRED |
CONSTANT INTEGER := 426; |
HTTP_PRECONDITION_REQUIRED |
CONSTANT INTEGER := 428; |
HTTP_TOO_MANY_REQUESTS |
CONSTANT INTEGER := 429; |
HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE |
CONSTANT INTEGER := 431; |
HTTP_NO_RESPONSE |
CONSTANT INTEGER := 444; |
HTTP_RETRY_WITH |
CONSTANT INTEGER := 449; |
HTTP_BLOCKED_BY_WINDOWS_PARENTAL_CONTROLS |
CONSTANT INTEGER := 450; |
HTTP_REDIRECT |
CONSTANT INTEGER := 451; |
HTTP_REQUEST_HEADER_TOO_LARGE |
CONSTANT INTEGER := 494; |
HTTP_CERT_ERROR |
CONSTANT INTEGER := 495; |
HTTP_NO_CERT |
CONSTANT INTEGER := 496; |
HTTP_HTTP_TO_HTTPS |
CONSTANT INTEGER := 497; |
HTTP_CLIENT_CLOSED_REQUEST |
CONSTANT INTEGER := 499; |
5XX SERVER ERROR |
|
|---|---|
HTTP_INTERNAL_SERVER_ERROR |
CONSTANT INTEGER := 500; |
HTTP_NOT_IMPLEMENTED |
CONSTANT INTEGER := 501; |
HTTP_BAD_GATEWAY |
CONSTANT INTEGER := 502; |
HTTP_SERVICE_UNAVAILABLE |
CONSTANT INTEGER := 503; |
HTTP_GATEWAY_TIME_OUT |
CONSTANT INTEGER := 504; |
HTTP_VERSION_NOT_SUPPORTED |
CONSTANT INTEGER := 505; |
HTTP_VARIANT_ALSO_NEGOTIATES |
CONSTANT INTEGER := 506; |
HTTP_INSUFFICIENT_STORAGE |
CONSTANT INTEGER := 507; |
HTTP_LOOP_DETECTED |
CONSTANT INTEGER := 508; |
HTTP_BANDWIDTH_LIMIT_EXCEEDED |
CONSTANT INTEGER := 509; |
HTTP_NOT_EXTENDED |
CONSTANT INTEGER := 510; |
HTTP_NETWORK_AUTHENTICATION_REQUIRED |
CONSTANT INTEGER := 511; |
HTTP_NETWORK_READ_TIME_OUT_ERROR |
CONSTANT INTEGER := 598; |
HTTP_NETWORK_CONNECT_TIME_OUT_ERROR |
CONSTANT INTEGER := 599; |
HTML_PIECES¶
The UTL_HTTP package declares a type named HTML_PIECES, which is a
table of type VARCHAR2 (2000) indexed by BINARY INTEGER. A value of this
type is returned by the REQUEST_PIECES function.
TYPE html_pieces IS TABLE OF VARCHAR2(2000) INDEX BY BINARY_INTEGER;
REQ¶
The REQ record type holds information about each HTTP request.
TYPE req IS RECORD (
url VARCHAR2(32767), -- URL to be accessed
method VARCHAR2(64), -- HTTP method
http_version VARCHAR2(64), -- HTTP version
private_hndl INTEGER -- Holds handle for this request
);
RESP¶
The RESP record type holds information about the response from each HTTP
request.
TYPE resp IS RECORD (
status_code INTEGER, -- HTTP status code
reason_phrase VARCHAR2(256), -- HTTP response reason phrase
http_version VARCHAR2(64), -- HTTP version
private_hndl INTEGER -- Holds handle for this response
);
BEGIN_REQUEST¶
The BEGIN_REQUEST function initiates a new HTTP request. A network
connection is established to the web server with the specified URL. The
signature is:
BEGIN_REQUEST(<url> IN VARCHAR2, <method> IN VARCHAR2 DEFAULT
'GET ', <http_version> IN VARCHAR2 DEFAULT NULL) RETURN
UTL_HTTP.REQ
The BEGIN_REQUEST function returns a record of type UTL_HTTP.REQ.
Parameters
url
urlis the Uniform Resource Locator from whichUTL_HTTPwill return content.
method
methodis the HTTP method to be used. The default isGET.
http_version
http_versionis the HTTP protocol version sending the request. The specified values should be eitherHTTP/1.0orHTTP/1.1. The default is null in which case the latest HTTP protocol version supported by theUTL_HTTPpackage is used which is 1.1.
END_REQUEST¶
The END_REQUEST procedure terminates an HTTP request. Use the
END_REQUEST procedure to terminate an HTTP request without completing
it and waiting for the response. The normal process is to begin the
request, get the response, then close the response. The signature is:
END_REQUEST(<r> IN OUT UTL_HTTP.REQ)
Parameters
r
ris the HTTP request record.
END_RESPONSE¶
The END_RESPONSE procedure terminates the HTTP response. The
END_RESPONSE procedure completes the HTTP request and response. This is
the normal method to end the request and response process. The signature
is:
END_RESPONSE(<r> IN OUT UTL_HTTP.RESP)
Parameters
r
ris theHTTPresponse record.
GET_BODY_CHARSET¶
The GET_BODY_CHARSET program is available in the form of both a
procedure and a function. A call to GET_BODY_CHARSET returns the
default character set of the body of future HTTP requests.
The procedure signature is:
GET_BODY_CHARSET(<charset> OUT VARCHAR2)
The function signature is:
GET_BODY_CHARSET() RETURN VARCHAR2
This function returns a VARCHAR2 value.
Parameters
charset
charsetis the character set of the body.
Examples
The following is an example of the GET_BODY_CHARSET function.
edb=# SELECT UTL_HTTP.GET_BODY_CHARSET() FROM DUAL;
get_body_charset
------------------
ISO-8859-1
(1 row)
GET_FOLLOW_REDIRECT¶
The GET_FOLLOW_REDIRECT procedure returns the current setting for the
maximum number of redirections allowed. The signature is:
GET_FOLLOW_REDIRECT(<max_redirects> OUT INTEGER)
Parameters
max_redirects
max_redirectsis maximum number of redirections allowed.
GET_HEADER¶
The GET_HEADER procedure returns the nth header of the HTTP
response. The signature is:
GET_HEADER(<r> IN OUT UTL_HTTP.RESP, <n> INTEGER, <name> OUT
VARCHAR2, <value> OUT VARCHAR2)
Parameters
r
ris the HTTP response record.
n
nis thenthheader of the HTTP response record to retrieve.
name
nameis the name of the response header.
value
valueis the value of the response header.
Examples
The following example retrieves the header count, then the headers.
DECLARE
v_req UTL_HTTP.REQ;
v_resp UTL_HTTP.RESP;
v_name VARCHAR2(30);
v_value VARCHAR2(200);
v_header_cnt INTEGER;
BEGIN
-- Initiate request and get response
v_req := UTL_HTTP.BEGIN_REQUEST('www.enterprisedb.com');
v_resp := UTL_HTTP.GET_RESPONSE(v_req);
-- Get header count
v_header_cnt := UTL_HTTP.GET_HEADER_COUNT(v_resp);
DBMS_OUTPUT.PUT_LINE('Header Count: ' || v_header_cnt);
-- Get all headers
FOR i IN 1 .. v_header_cnt LOOP
UTL_HTTP.GET_HEADER(v_resp, i, v_name, v_value);
DBMS_OUTPUT.PUT_LINE(v_name || ': ' || v_value);
END LOOP;
-- Terminate request
UTL_HTTP.END_RESPONSE(v_resp);
END;
The following is the output from the example.
Header Count: 23
Age: 570
Cache-Control: must-revalidate
Content-Type: text/html; charset=utf-8
Date: Wed, 30 Apr 2015 14:57:52 GMT
ETag: "aab02f2bd2d696eed817ca89ef411dda"
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Last-Modified: Wed, 30 Apr 2015 14:15:49 GMT
RTSS: 1-1307-3
Server: Apache/2.2.3 (Red Hat)
Set-Cookie:
SESS2771d0952de2a1a84d322a262e0c173c=jn1u1j1etmdi5gg4lh8hakvs01;
expires=Fri, 23-May-2015 18:21:43 GMT; path=/; domain=.enterprisedb.com
Vary: Accept-Encoding
Via: 1.1 varnish
X-EDB-Backend: ec
X-EDB-Cache: HIT
X-EDB-Cache-Address: 10.31.162.212
X-EDB-Cache-Server: ip-10-31-162-212
X-EDB-Cache-TTL: 600.000
X-EDB-Cacheable: MAYBE: The user has a cookie of some sort. Maybe it's
double choc-chip!
X-EDB-Do-GZIP: false
X-Powered-By: PHP/5.2.17
X-Varnish: 484508634 484506789
transfer-encoding: chunked
Connection: keep-alive
GET_HEADER_BY_NAME¶
The GET_HEADER_BY_NAME procedure returns the header of the HTTP
response according to the specified name. The signature is:
GET_HEADER_BY_NAME(<r> IN OUT UTL_HTTP.RESP, <name> VARCHAR2,
<value> OUT VARCHAR2, <n> INTEGER DEFAULT 1)
Parameters
r
ris the HTTP response record.
name
nameis the name of the response header to retrieve.
value
valueis the value of the response header.
n
nis thenthheader of the HTTP response record to retrieve according to the values specified byname. The default is 1.
Examples
The following example retrieves the header for Content-Type.
DECLARE
v_req UTL_HTTP.REQ;
v_resp UTL_HTTP.RESP;
v_name VARCHAR2(30) := 'Content-Type';
v_value VARCHAR2(200);
BEGIN
v_req := UTL_HTTP.BEGIN_REQUEST('www.enterprisedb.com');
v_resp := UTL_HTTP.GET_RESPONSE(v_req);
UTL_HTTP.GET_HEADER_BY_NAME(v_resp, v_name, v_value);
DBMS_OUTPUT.PUT_LINE(v_name || ': ' || v_value);
UTL_HTTP.END_RESPONSE(v_resp);
END;
Content-Type: text/html; charset=utf-8
GET_HEADER_COUNT¶
The GET_HEADER_COUNT function returns the number of HTTP response
headers. The signature is:
GET_HEADER_COUNT(<r> IN OUT UTL_HTTP.RESP) RETURN INTEGER
This function returns an INTEGER value.
Parameters
r
ris the HTTP response record.
GET_RESPONSE¶
The GET_RESPONSE function sends the network request and returns any
HTTP response. The signature is:
GET_RESPONSE(<r> IN OUT UTL_HTTP.REQ) RETURN UTL_HTTP.RESP
This function returns a UTL_HTTP.RESP record.
Parameters
r
ris the HTTP request record.
GET_RESPONSE_ERROR_CHECK¶
The GET_RESPONSE_ERROR_CHECK procedure returns whether or not response error check is set. The signature is:
GET_RESPONSE_ERROR_CHECK(<enable> OUT BOOLEAN)
Parameters
enable
enablereturnsTRUEif response error check is set, otherwise it returnsFALSE.
GET_TRANSFER_TIMEOUT¶
The GET_TRANSFER_TIMEOUT procedure returns the current, default transfer timeout setting for HTTP requests. The signature is:
GET_TRANSFER_TIMEOUT(<timeout> OUT INTEGER)
Parameters
timeout
timeoutis the transfer timeout setting in seconds.
READ_LINE¶
The READ_LINE procedure returns the data from the HTTP response body in
text form until the end of line is reached. A CR character, a LF
character, a CR LF sequence, or the end of the response body constitutes
the end of line. The signature is:
READ_LINE(<r> IN OUT UTL_HTTP.RESP, <data> OUT VARCHAR2,
<remove_crlf> BOOLEAN DEFAULT FALSE)
Parameters
r
ris the HTTP response record.
data
datais the response body in text form.
remove_crlf
Set
remove_crlftoTRUEto remove new line characters, otherwise set toFALSE. The default isFALSE.
Examples
The following example retrieves and displays the body of the specified website.
DECLARE
v_req UTL_HTTP.REQ;
v_resp UTL_HTTP.RESP;
v_value VARCHAR2(1024);
BEGIN
v_req := UTL_HTTP.BEGIN_REQUEST('http://www.enterprisedb.com');
v_resp := UTL_HTTP.GET_RESPONSE(v_req);
LOOP
UTL_HTTP.READ_LINE(v_resp, v_value, TRUE);
DBMS_OUTPUT.PUT_LINE(v_value);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
UTL_HTTP.END_RESPONSE(v_resp);
END;
The following is the output.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<!-- ___________________________ HEAD ___________________________ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>EnterpriseDB | The Postgres Database Company</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="postgres, postgresql, postgresql installer,
mysql migration, open source database, training, replication" />
<meta name="description" content="The leader in open source database
products, services, support, training and expertise based on PostgreSQL.
Free downloads, documentation, and tutorials." />
<meta name="abstract" content="The Enterprise PostgreSQL Company" />
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://
www.enterprisedb.com/blogapi/rsd" />
<link rel="alternate" type="application/rss+xml" title="EnterpriseDB RSS"
href="http://www.enterprisedb.com/rss.xml" />
<link rel="shortcut icon" href="/sites/all/themes/edb_pixelcrayons/
favicon.ico" type="image/x-icon" />
<link type="text/css" rel="stylesheet" media="all" href="/sites/default/
files/css/css_db11adabae0aed6b79a2c3c52def4754.css" />
<!--[if IE 6]>
<link type="text/css" rel="stylesheet" media="all" href="/sites/all/themes/
oho_basic/css/ie6.css?g" />
<![endif]-->
<!--[if IE 7]>
<link type="text/css" rel="stylesheet" media="all" href="/sites/all/themes/
oho_basic/css/ie7.css?g" />
<![endif]-->
<script type="text/javascript" src="/sites/default/files/js/
js_74d97b1176812e2fd6e43d62503a5204.js"></script>
<script type="text/javascript">
<!--//--><![CDATA[//><!--
READ_RAW¶
The READ_RAW procedure returns the data from the HTTP response body in
binary form. The number of bytes returned is specified by the len
parameter. The signature is:
READ_RAW(<r> IN OUT UTL_HTTP.RESP, <data> OUT RAW, <len> INTEGER)
Parameters
r
ris the HTTP response record.
data
datais the response body in binary form.
len
Set
lento the number of bytes of data to be returned.
Examples
The following example retrieves and displays the first 150 bytes in binary form.
DECLARE
v_req UTL_HTTP.REQ;
v_resp UTL_HTTP.RESP;
v_data RAW;
BEGIN
v_req := UTL_HTTP.BEGIN_REQUEST('http://www.enterprisedb.com');
v_resp := UTL_HTTP.GET_RESPONSE(v_req);
UTL_HTTP.READ_RAW(v_resp, v_data, 150);
DBMS_OUTPUT.PUT_LINE(v_data);
UTL_HTTP.END_RESPONSE(v_resp);
END;
The following is the output from the example.
\x3c21444f43545950452068746d6c205055424c494320222d2f2f5733432f2f4454442058485
44d4c20312e30205374726963742f2f454e220d0a202022687474703a2f2f7777772e77332e6f
72672f54522f7868746d6c312f4454442f7868746d6c312d7374726963742e647464223e0d0a3
c68746d6c20786d6c6e733d22687474703a2f2f7777772e77332e6f72672f313939392f
READ_TEXT¶
The READ_TEXT procedure returns the data from the HTTP response body in
text form. The maximum number of characters returned is specified by the
len parameter. The signature is:
READ_TEXT(<r> IN OUT UTL_HTTP.RESP, <data> OUT VARCHAR2, <len> INTEGER)
Parameters
r
ris the HTTP response record.
data
datais the response body in text form.
len
Set
lento the maximum number of characters to be returned.
Examples
The following example retrieves the first 150 characters.
DECLARE
v_req UTL_HTTP.REQ;
v_resp UTL_HTTP.RESP;
v_data VARCHAR2(150);
BEGIN
v_req := UTL_HTTP.BEGIN_REQUEST('http://www.enterprisedb.com');
v_resp := UTL_HTTP.GET_RESPONSE(v_req);
UTL_HTTP.READ_TEXT(v_resp, v_data, 150);
DBMS_OUTPUT.PUT_LINE(v_data);
UTL_HTTP.END_RESPONSE(v_resp);
END;
The following is the output.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/
REQUEST¶
The REQUEST function returns the first 2000 bytes retrieved from a
user-specified URL. The signature is:
REQUEST(<url> IN VARCHAR2) RETURN VARCHAR2
If the data found at the given URL is longer than 2000 bytes, the remainder will be discarded. If the data found at the given URL is shorter than 2000 bytes, the result will be shorter than 2000 bytes.
Parameters
url
urlis the Uniform Resource Locator from whichUTL_HTTPwill return content.
Example
The following command returns the first 2000 bytes retrieved from the EDB website:
SELECT UTL_HTTP.REQUEST('http://www.enterprisedb.com/') FROM DUAL;
REQUEST_PIECES¶
The REQUEST_PIECES function returns a table of 2000-byte segments
retrieved from an URL. The signature is:
REQUEST_PIECES(<url> IN VARCHAR2, <max_pieces> NUMBER IN
DEFAULT 32767) RETURN UTL_HTTP.HTML_PIECES
Parameters
url
urlis the Uniform Resource Locator from whichUTL_HTTPwill return content.
max_pieces
max_piecesspecifies the maximum number of 2000-byte segments that theREQUEST_PIECESfunction will return. Ifmax_piecesspecifies more units than are available at the specifiedurl, the final unit will contain fewer bytes.
Example
The following example returns the first four 2000 byte segments retrieved from the EDB website:
DECLARE
result UTL_HTTP.HTML_PIECES;
BEGIN
result := UTL_HTTP.REQUEST_PIECES('http://www.enterprisedb.com/', 4);
END
SET_BODY_CHARSET¶
The SET_BODY_CHARSET procedure sets the default character set of the
body of future HTTP requests. The signature is:
SET_BODY_CHARSET(<charset> VARCHAR2 DEFAULT NULL)
Parameters
charset
charsetis the character set of the body of future requests. The default is null in which case the database character set is assumed.
SET_FOLLOW_REDIRECT¶
The SET_FOLLOW_REDIRECT procedure sets the maximum number of times the
HTTP redirect instruction is to be followed in the response to this request or future requests. This procedures has two signatures:
SET_FOLLOW_REDIRECT(<max_redirects> IN INTEGER DEFAULT 3)
and
SET_FOLLOW_REDIRECT(<r> IN OUT UTL_HTTP.REQ, <max_redirects>
IN INTEGER DEFAULT 3)``
Use the second form to change the maximum number of redirections for an individual request that a request inherits from the session default settings.
Parameters
r
ris the HTTP request record.
max_redirects
max_redirectsis maximum number of redirections allowed. Set to 0 to disable redirections. The default is 3.
SET_HEADER¶
The SET_HEADER procedure sets the HTTP request header. The signature
is:
SET_HEADER(<r> IN OUT UTL_HTTP.REQ, <name> IN VARCHAR2, <value>
IN VARCHAR2 DEFAULT NULL)
Parameters
r
ris the HTTP request record.
name
nameis the name of the request header.
value
valueis the value of the request header. The default is null.
SET_RESPONSE_ERROR_CHECK¶
The SET_RESPONSE_ERROR_CHECK procedure determines whether or not HTTP
4xx and 5xx status codes returned by the GET_RESPONSE function should
be interpreted as errors. The signature is:
SET_RESPONSE_ERROR_CHECK(<enable> IN BOOLEAN DEFAULT FALSE)
Parameters
enable
Set
enabletoTRUEif HTTP 4xx and 5xx status codes are to be treated as errors, otherwise set toFALSE. The default isFALSE.
SET_TRANSFER_TIMEOUT¶
The SET_TRANSFER_TIMEOUT procedure sets the default, transfer timeout
setting for waiting for a response from an HTTP request. This procedure has two signatures:
SET_TRANSFER_TIMEOUT(<timeout> IN INTEGER DEFAULT 60)
and
SET_TRANSFER_TIMEOUT(<r> IN OUT UTL_HTTP.REQ, <timeout> IN
INTEGER DEFAULT 60)
Use the second form to change the transfer timeout setting for an individual request that a request inherits from the session default settings.
Parameters
r
ris the HTTP request record.
timeout
timeoutis the transfer timeout setting in seconds for HTTP requests. The default is 60 seconds.
WRITE_LINE¶
The WRITE_LINE procedure writes data to the HTTP request body in text
form; the text is terminated with a CRLF character pair. The signature is:
WRITE_LINE(<r> IN OUT UTL_HTTP.REQ, <data> IN VARCHAR2)
Parameters
r
ris the HTTP request record.
data
datais the request body inTEXTform.
Example
The following example writes data (Account balance $500.00) in text form
to the request body to be sent using the HTTP POST method. The data is
sent to a hypothetical web application (post.php) that accepts and
processes data.
DECLARE
v_req UTL_HTTP.REQ;
v_resp UTL_HTTP.RESP;
BEGIN
v_req := UTL_HTTP.BEGIN_REQUEST('http://www.example.com/post.php',
'POST');
UTL_HTTP.SET_HEADER(v_req, 'Content-Length', '23');
UTL_HTTP.WRITE_LINE(v_req, 'Account balance $500.00');
v_resp := UTL_HTTP.GET_RESPONSE(v_req);
DBMS_OUTPUT.PUT_LINE('Status Code: ' || v_resp.status_code);
DBMS_OUTPUT.PUT_LINE('Reason Phrase: ' || v_resp.reason_phrase);
UTL_HTTP.END_RESPONSE(v_resp);
END;
Assuming the web application successfully processed the POST method, the following output would be displayed:
Status Code: 200
Reason Phrase: OK
WRITE_RAW¶
The WRITE_RAW procedure writes data to the HTTP request body in binary
form. The signature is:
WRITE_RAW(<r> IN OUT UTL_HTTP.REQ, <data> IN RAW)
Parameters
r
ris the HTTP request record.
data
datais the request body in binary form.
Example
The following example writes data in binary form to the request body to
be sent using the HTTP POST method to a hypothetical web application that accepts and processes such data.
DECLARE
v_req UTL_HTTP.REQ;
v_resp UTL_HTTP.RESP;
BEGIN
v_req := UTL_HTTP.BEGIN_REQUEST('http://www.example.com/post.php',
'POST');
UTL_HTTP.SET_HEADER(v_req, 'Content-Length', '23');
UTL_HTTP.WRITE_RAW(v_req, HEXTORAW
('54657374696e6720504f5354206d6574686f6420696e20485454502072657175657374'));
v_resp := UTL_HTTP.GET_RESPONSE(v_req);
DBMS_OUTPUT.PUT_LINE('Status Code: ' || v_resp.status_code);
DBMS_OUTPUT.PUT_LINE('Reason Phrase: ' || v_resp.reason_phrase);
UTL_HTTP.END_RESPONSE(v_resp);
END;
The text string shown in the HEXTORAW function is the hexadecimal
translation of the text Testing POST method in HTTP request.
Assuming the web application successfully processed the POST method, the
following output would be displayed:
Status Code: 200
Reason Phrase: OK
WRITE_TEXT¶
The WRITE_TEXT procedure writes data to the HTTP request body in text
form. The signature is:
WRITE_TEXT(<r> IN OUT UTL_HTTP.REQ, <data> IN VARCHAR2)
Parameters
r
ris the HTTP request record.
data
datais the request body in text form.
Example
The following example writes data (Account balance $500.00) in text form
to the request body to be sent using the HTTP POST method. The data is
sent to a hypothetical web application (post.php) that accepts and
processes data.
DECLARE
v_req UTL_HTTP.REQ;
v_resp UTL_HTTP.RESP;
BEGIN
v_req := UTL_HTTP.BEGIN_REQUEST('http://www.example.com/post.php',
'POST');
UTL_HTTP.SET_HEADER(v_req, 'Content-Length', '23');
UTL_HTTP.WRITE_TEXT(v_req, 'Account balance $500.00');
v_resp := UTL_HTTP.GET_RESPONSE(v_req);
DBMS_OUTPUT.PUT_LINE('Status Code: ' || v_resp.status_code);
DBMS_OUTPUT.PUT_LINE('Reason Phrase: ' || v_resp.reason_phrase);
UTL_HTTP.END_RESPONSE(v_resp);
END;
Assuming the web application successfully processed the POST method, the
following output would be displayed:
Status Code: 200
Reason Phrase: OK
END_OF_BODY¶
The END_OF_BODY exception will be raised when it reaches the end of the HTTP response body.
Example
The following example handles the exception and writes Exception caught in text form to the request body to be sent using the HTTP POST method. The data is sent to a hypothetical web application (post.php) that accepts and
processes data.
DECLARE
req UTL_HTTP.REQ;
resp UTL_HTTP.RESP;
value VARCHAR2(32768);
BEGIN
req := UTL_HTTP.BEGIN_REQUEST('https://www.google.com/');
resp := UTL_HTTP.GET_RESPONSE(req);
LOOP
UTL_HTTP.READ_LINE(resp, value, TRUE);
END LOOP;
UTL_HTTP.END_RESPONSE(resp);
EXCEPTION
WHEN UTL_HTTP.END_OF_BODY THEN
DBMS_OUTPUT.PUT_LINE('Exception caught');
UTL_HTTP.END_RESPONSE(resp);
END;
Assuming the web application successfully processed the POST method, the
following output will be displayed:
Output is:
Exception caught