Table of Contents
This chapter briefly introduces the UTL_HTTP package based on HTTP/1.1, and describes how to use the procedures and functions of the package.
UTL_HTTP provides client roles based on the web standard (RFC2616) HTTP protocol. It offers subprograms for manipulating the protocol at low-level or simply getting a web page.
HTTP version type.
Details about the HTTP version constants are as follows:
HTTP_VERSION_1 CONSTANT VARCHAR2(10) := 'HTTP/1.1'; DEFAULT_HTTPS_PORT CONSTANT PLS_INTEGER := 443;
Network port number used in HTTP.
Details about the HTTP port constants are as follows:
DEFAULT_HTTP_PORT CONSTANT PLS_INTEGER := 80; DEFAULT_HTTPS_PORT CONSTANT PLS_INTEGER := 443;
Status of a response received from the server through the HTTP protocol.
Details about the status code constants are as follows in alphabetical order:
HTTP_CONTINUE CONSTANT PLS_INTEGER := 100; HTTP_SWITCHING_PROTOCOLS CONSTANT PLS_INTEGER := 101; HTTP_OK CONSTANT PLS_INTEGER := 200; HTTP_CREATED CONSTANT PLS_INTEGER := 201; HTTP_ACCEPTED CONSTANT PLS_INTEGER := 202; HTTP_NON_AUTHORITATIVE_INFO CONSTANT PLS_INTEGER := 203; HTTP_NO_CONTENT CONSTANT PLS_INTEGER := 204; HTTP_RESET_CONTENT CONSTANT PLS_INTEGER := 205; HTTP_PARTIAL_CONTENT CONSTANT PLS_INTEGER := 206; HTTP_MULTIPLE_CHOICES CONSTANT PLS_INTEGER := 300; HTTP_MOVED_PERMANENTLY CONSTANT PLS_INTEGER := 301; HTTP_FOUND CONSTANT PLS_INTEGER := 302; HTTP_SEE_OTHER CONSTANT PLS_INTEGER := 303; HTTP_NOT_MODIFIED CONSTANT PLS_INTEGER := 304; HTTP_USE_PROXY CONSTANT PLS_INTEGER := 305; HTTP_TEMPORARY_REDIRECT CONSTANT PLS_INTEGER := 307; HTTP_BAD_REQUEST CONSTANT PLS_INTEGER := 400; HTTP_UNAUTHORIZED CONSTANT PLS_INTEGER := 401; HTTP_PAYMENT_REQUIRED CONSTANT PLS_INTEGER := 402; HTTP_FORBIDDEN CONSTANT PLS_INTEGER := 403; HTTP_NOT_FOUND CONSTANT PLS_INTEGER := 404; HTTP_NOT_ACCEPTABLE CONSTANT PLS_INTEGER := 406; HTTP_PROXY_AUTH_REQUIRED CONSTANT PLS_INTEGER := 407; HTTP_REQUEST_TIME_OUT CONSTANT PLS_INTEGER := 408; HTTP_CONFLICT CONSTANT PLS_INTEGER := 409; HTTP_GONE CONSTANT PLS_INTEGER := 410; HTTP_LENGTH_REQUIRED CONSTANT PLS_INTEGER := 411; HTTP_PRECONDITION_FAILED CONSTANT PLS_INTEGER := 412; HTTP_REQUEST_ENTITY_TOO_LARGE CONSTANT PLS_INTEGER := 413; HTTP_REQUEST_URI_TOO_LARGE CONSTANT PLS_INTEGER := 414; HTTP_UNSUPPORTED_MEDIA_TYPE CONSTANT PLS_INTEGER := 415; HTTP_REQ_RANGE_NOT_SATISFIABLE CONSTANT PLS_INTEGER := 416; HTTP_EXPECTATION_FAILED CONSTANT PLS_INTEGER := 417; HTTP_NOT_IMPLEMENTED CONSTANT PLS_INTEGER := 501; HTTP_BAD_GATEWAY CONSTANT PLS_INTEGER := 502; HTTP_SERVICE_UNAVAILABLE CONSTANT PLS_INTEGER := 503; HTTP_GATEWAY_TIME_OUT CONSTANT PLS_INTEGER := 504; HTTP_VERSION_NOT_SUPPORTED CONSTANT PLS_INTEGER := 505;
This section describes the types provided by the UTL_HTTP package, in alphabetical order.
Used to get a page from the server in the string array type.
Details about the HTML_PIECES type are as follows:
Prototype
TYPE html_pieces IS TABLE OF VARCHAR2(2000) INDEX BY BINARY_INTEGER;
Used to contain request information to send to the server.
Details about the req type are as follows:
Prototype
TYPE req IS RECORD ( url VARCHAR2(32767), method VARCHAR2(64), http_version VARCHAR2(64) );
Field
Name | Description |
---|---|
url | Web page address. For more information, refer to “56.7. URL”. |
method | Method to request to the server. Available values are 'OPTIONS', 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'TRACE', and 'CONNECT'. (Others are unsupported) |
http_version | Protocol version. For more information about available values, refer to “56.2.1. HTTP Version Constant”. |
Used to contain status information about a response received from the server.
Details about the resp type are as follows:
Prototype
TYPE resp IS RECORD ( status_code PLS_INTEGER, reason_phrase VARCHAR2(256), http_version VARCHAR2(64) );
Field
Name | Description |
---|---|
status_code | 3-digit number that expresses the response status of a request. For more information about available values, refer to “56.2.3. Status Code Constants”. |
reason_phrase | Brief description about the status code. |
http_version | Protocol version. For more information about available values, refer to “56.2.1. HTTP Version Constant”. |
The following are exceptions provided by the UTL_HTTP package. For more information, refer to "Exceptions" in “56.5. Functions” and “56.6. Procedures”.
BAD_ARGUMENT
END_OF_BODY
REQUEST_FAILED
This section describes the functions provided by the UTL_HTTP package, in alphabetical order.
Establishes a connection to the web server, and determines the method used to send a request to the server.
Details about the BEGIN_REQUEST function are as follows:
Prototype
FUNCTION BEGIN_REQUEST ( url IN VARCHAR2, method IN VARCHAR2 DEFAULT 'GET', http_version IN VARCHAR2 DEFAULT NULL ) RETURN req;
Parameter
Parameter | Description |
---|---|
url | Web page address. For more information, refer to “56.7. URL”. |
method | Method used to send a request to the server. Available values are 'OPTIONS', 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'TRACE', and 'CONNECT'. (Others are unsupported) |
http_version | Protocol version. For more information about available values, refer to “56.2.1. HTTP Version Constant”. |
Exception
Exception | Description |
---|---|
REQUEST_FAILED | Occurs when communication with the web server fails due to an issue with the request. Check if URL is correct and the web server is running normally. |
PKG_TRANSFER_TIMEOUT | Occurs when the web server does not respond for more than 20 seconds. |
Example
This function cannot be used alone. Refer to “56.8. Example”.
Sends a request to the web server, reads the response header, and prepares to read the message body. If the message header contains "Content-Length: length", it reads the message body up to the specified length. If it contains "Transfer-Encoding: Chunked", the message body is read until a carriage return or line feed character is reached. If it contains both, the former is ignored.
Details about the GET_RESPONSE function are as follows:
Prototype
FUNCTION GET_RESPONSE ( r IN OUT NOCOPY req ) RETURN resp;
Parameter
Parameter | Description |
---|---|
r | Request identifier. |
Exception
Exception | Description |
---|---|
BAD_ARGUMENT | Occurs when this function is called without executing BEGIN_REQUEST or SET_HEADER, or a parameter is used incorrectly. |
Example
This function cannot be used alone. Refer to “56.8. Example”.
Sends a request to the specified URL on the web server using the GET method of the HTTP/1.1 standard and returns up to 2,000 bytes of data.
Details about the REQUEST function are as follows:
Prototype
FUNCTION REQUEST ( url IN VARCHAR2, proxy IN VARCHAR2 DEFAULT NULL, wallet_path IN VARCHAR2 DEFAULT NULL, wallet_password IN VARCHAR2 DEFAULT NULL ) RETURN VARCHAR2;
Parameter
Parameter | Description |
---|---|
url | Web page address. For more information, refer to “56.7. URL”. |
proxy | Proxy server name. (Default value: NULL, planned for future versions) |
wallet_path | Path in which a wallet required for a client to use HTTPS is saved. (Default value: NULL, planned for future versions) |
wallet_password | Password required to open the wallet. (Default value: NULL, planned for future versions) |
Exception
Exception | Description |
---|---|
REQUEST_FAILED | Occurs when communication with the web server fails due to an issue with the request. Check if URL is correct and the web server is running normally. |
PKG_TRANSFER_TIMEOUT | Occurs when the web server does not respond for more than 20 seconds. |
Example
DECLARE resp VARCHAR2(32767); BEGIN resp := UTL_HTTP.REQUEST('http://www.w3.org/Protocols/'); END;
This section describes the procedures provided by the UTL_HTTP package, in alphabetical order.
Ends an HTTP request by returning and initializing all resources such as buffer memory and socket.
Details about the END_RESPONSE procedure are as follows:
Prototype
PROCEDURE END_RESPONSE ( r IN OUT NOCOPY resp );
Parameter
Parameter | Description |
---|---|
r | HTTP response. |
Example
This procedure cannot be used alone. Refer to “56.8. Example”.
Gets the name of the default character set of the message body configured in the session.
Details about the GET_BODY_CHARSET procedure are as follows:
Prototype
PROCEDURE GET_BODY_CHARSET ( charset OUT NOCOPY VARCHAR2 );
Parameter
Parameter | Description |
---|---|
charset | Name of the character set configured in the session (canonical character set name). |
Example
DECLARE
charset varchar(64);
BEGIN
UTL_HTTP.GET_BODY_CHARSET(charset); -- Canonical character set name
END;
/
Gets whether the UTL_HTTP package will raise a detailed exception during execution. This information is set in each session.
Details about the GET_DETAILED_EXCP_SUPPORT procedure are as follows:
Prototype
PROCEDURE GET_DETAILED_EXCP_SUPPORT ( enable OUT BOOLEAN );
Parameter
Parameter | Description |
---|---|
enable | Indicates whether detailed exception is enabled. |
Example
DECLARE
enable BOOLEAN;
BEGIN
UTL_HTTP.GET_DETAILED_EXCP_SUPPORT(enable); -- Enable detailed exception
END;
/
Gets timeout setting of the current session.
Details about the GET_TRANSFER_TIMEOUT procedure are as follows:
Prototype
PROCEDURE GET_TRANSFER_TIMEOUT ( timeout OUT PLS_INTEGER );
Parameter
Parameter | Description |
---|---|
timeout | Timeout setting of the session. (Default value: 50 seconds) |
Example
DECLARE timeout PLS_INTEGER; BEGIN utl_http.get_transfer_timeout(var); END;
Reads in the body of the requested page without the header.
Details about the READ_TEXT procedure are as follows:
Prototype
PROCEDURE READ_TEXT ( r IN OUT NOCOPY resp, data OUT NOCOPY VARCHAR2, len IN PLS_INTEGER DEFAULT NULL );
Parameter
Parameter | Description |
---|---|
r | Request identifier. |
data | Body without the header. |
len | Maximum string length to read. The value must be greater than 0. If the remaining string length is less than this value, the actual length of the string is read. |
Exception
Exception | Description |
---|---|
BAD_ARGUMENT | Occurs when this procedure is called without executing GET_RESPONSE, or a parameter is used incorrectly. |
END_OF_BODY | Occurs when a read is executed after the end of the body has been reached. |
Example
This procedure cannot be used alone. Refer to “56.8. Example”.
Sets the character set of an HTTP request or response message body.
Details about the SET_BODY_CHARSET procedure are as follows:
CASE1
Specifies the character set for a session when the character set is not specified in an HTTP request or response and the Content-Type is not specified in the message header. If the Content-Type is specified in the message header, this setting is ignored. This can be set for each session.
Prototype
PROCEDURE SET_BODY_CHARSET ( charset IN VARCHAR2 DEFAULT NULL );
Parameter
Parameter | Description |
---|---|
charset | Canonical character set name. (Default value: ISO-8859-1) (Example: ASCII, UTF-8, EUC-KR, CP949) |
CASE2
Specifies the character set for HTTP request message body when the Content-Type is not specified in the message header (default value: ISO-8859-1). The message body string expressed using a database character set is automatically converted to the specified character set. If the Content-Type is specified in the message header, this setting is ignored. The character set can be specified for a single HTTP request.
Prototype
PROCEDURE SET_BODY_CHARSET ( r IN OUT NOCOPY req, charset IN VARCHAR2 DEFAULT NULL );
Parameters
Parameter | Description |
---|---|
charset | Canonical character set name. (Default value: ISO-8859-1) (Example: ASCII, UTF-8, EUC-KR, CP949) |
req | HTTP request. |
CASE3
Specifies the character set for HTTP response message body when the Content-Type is not specified in the message header (default value: ISO-8859-1). The message body string expressed using the specified character set is automatically converted to a database character set. If the Content-Type is specified in the message header, this setting is ignored. The character set can be specified for a single HTTP response.
Prototype
PROCEDURE SET_BODY_CHARSET ( r IN OUT NOCOPY resp, charset IN VARCHAR2 DEFAULT NULL );
Parameters
Parameter | Description |
---|---|
charset | Canonical character set name. (Default value: ISO-8859-1) (Example: ASCII, UTF-8, EUC-KR, CP949) |
resp | HTTP response. |
Exception
Exception | Description |
---|---|
BAD_ARGUMENT | Occurs when the specified character set is invalid. |
Example
-- Change the character set for HTTP request/response to UTF-8. DECLARE req UTL_HTTP.REQ; resp UTL_HTTP.RESP; value varchar(32767); BEGIN req := UTL_HTTP.BEGIN_REQUEST('http://www.naver.com', 'GET','HTTP/1.1'); UTL_HTTP.SET_HEADER(req, 'User-Agent', 'Mozilla/4.0'); UTL_HTTP.WRITE_TEXT(req, 'test_value'); UTL_HTTP.SET_BODY_CHARSET(req, 'UTF-8'); resp := UTL_HTTP.GET_RESPONSE(req); UTL_HTTP.SET_BODY_CHARSET(resp, 'UTF-8'); FOR i in 1 .. 200000 LOOP UTL_HTTP.READ_TEXT(resp, value); DBMS_OUTPUT.PUT_LINE ( value ); END LOOP; UTL_HTTP.END_RESPONSE(resp); exception when others then UTL_HTTP.END_RESPONSE(resp); end; / -- Change the default character set of a session for HTTP requests/responses to UTF-8. BEGIN UTL_HTTP.SET_BODY_CHARSET('UTF-8'); END; /
Sets whether the UTL_HTTP package will raise a detailed exception during execution. This information is set in each session.
Details about the SET_DETAILED_EXCP_SUPPORT procedure are as follows:
Prototype
PROCEDURE SET_DETAILED_EXCP_SUPPORT ( enable OUT BOOLEAN DEFAULT FALSE );
Parameter
Parameter | Description |
---|---|
enable | Option to enable detailed exception. |
Example
BEGIN
UTL_HTTP.SET_DETAILED_EXCP_SUPPORT(false); -- disabled
END;
/
Sets a request header to send to the server. This procedure can be executed multiple time to add additional header information.
Details about the SET_HEADER procedure are as follows:
Prototype
PROCEDURE SET_HEADER ( r IN OUT NOCOPY req, name IN VARCHAR2, value IN VARCHAR2 );
Parameter
Parameter | Description |
---|---|
r | Request identifier. |
name | Header name. |
value | Header value. |
Example
This procedure cannot be used alone. Refer to “56.8. Example”.
Sets whether GET_RESPONSE handles a status code as an exception if the status code received from a web server is about an error. Status codes between 400 and 599 are about an error.
Details about the SET_RESPONSE_ERROR_CHECK procedure are as follows:
Prototype
PROCEDURE SET_RESPONSE_ERROR_CHECK ( enable IN BOOLEAN DEFAULT FALSE );
Parameter
Parameter | Description |
---|---|
enable | Option to enable the function of checking a status error. (Default value: FALSE) |
Example
DECLARE
req UTL_HTTP.REQ;
resp UTL_HTTP.RESP;
value VARCHAR2(1024);
BEGIN
UTL_HTTP.SET_RESPONSE_ERROR_CHECK(enable => TRUE); -- enabled
req := UTL_HTTP.BEGIN_REQUEST('http://getstatuscode.com/400','GET', 'HTTP/1.1');
UTL_HTTP.SET_HEADER(req, 'User-Agent', 'Mozilla/4.0');
resp := UTL_HTTP.GET_RESPONSE(req);
UTL_HTTP.END_RESPONSE(resp);
END;
/
Sets timeout for the current session.
Details about the SET_TRANSFER_TIMEOUT procedure are as follows:
Prototype
PROCEDURE SET_TRANSFER_TIMEOUT(timeout IN PLS_INTEGER);
Parameter
Parameter | Description |
---|---|
timeout | Timeout setting for the session. (Default value: 50 seconds) |
Example
BEGIN utl_http.set_transfer_timeout(30); END; /
Sets a request body to send to the server.
Details about the WRITE_TEXT procedure are as follows:
Prototype
PROCEDURE WRITE_TEXT ( r IN OUT NOCOPY REQ, data IN VARCHAR2 );
Parameter
Parameter | Description |
---|---|
r | Request identifier. |
data | Body text. |
Exception
Exception | Description |
---|---|
BAD_ARGUMENT | Occurs when the procedure is called without executing BEGIN_REQUEST or SET_HEADER, or a parameter is used incorrectly. |
END_OF_BODY | Occurs when a read is executed after the end of the body has been reached. |
Example
This procedure cannot be used alone. Refer to “56.8. Example”.
URL (Uniform Resource Locator) expresses the server web page address in the following format.
scheme://[user[password]@]host[:port]/[...]
The following describes each item.
Item | Description |
---|---|
scheme | Available values are http and https, but https is planned for use in future versions. |
user | Used for a proxy server. Planned for future versions. |
password | Used for a proxy server. Planned for future versions. |
host | HTTP server host name. |
port | Port number used for communication. |
The following reads in an html script from the login page of ims.tmaxsoft.com using BEGIN_REQUEST, SET_HEADER, WRITE_TEXT, GET_RESPONSE, and END_RESPONSE.
DECLARE req UTL_HTTP.REQ; resp UTL_HTTP.RESP; value VARCHAR2(1024); BEGIN req := UTL_HTTP.BEGIN_REQUEST('http://ims.tmaxsoft.com/tody/auth/login.do', 'POST', 'HTTP/1.1'); UTL_HTTP.SET_HEADER(req, 'User-Agent', 'Mozilla/4.0'); UTL_HTTP.WRITE_TEXT(req, 'test_value'); resp := UTL_HTTP.GET_RESPONSE(req); LOOP UTL_HTTP.READ_TEXT(resp, value); DBMS_OUTPUT.PUT_LINE ( value ); END LOOP; EXCEPTION WHEN UTL_HTTP.END_OF_BODY THEN UTL_HTTP.END_RESPONSE(resp); END; /