
Many organizations run critical business logic inside Oracle databases. Sometimes that logic needs data from outside the database, such as currency exchange rates, address validation, shipping quotes, or updates from a cloud application. Rather than building a separate middleware layer, Oracle allows developers to call web services directly from PL/SQL. This capability is powerful, but it requires proper network configuration, security setup, and careful coding. This guide explains how to call a web service in Oracle PL/SQL using the UTL_HTTP package and the APEX_WEB_SERVICE API.
Connect Your Databases and Applications With AAMAX.CO
Database-driven integrations require developers who understand both enterprise data systems and modern web APIs. AAMAX.CO is a full-service digital marketing company offering web development, digital marketing, and SEO services worldwide. Through their web application development services, they design integrations that connect databases, back-end services, and web front ends securely and efficiently. Their team helps businesses modernize legacy systems without disrupting daily operations.
Step 1: Grant Network Access With an ACL
Since Oracle 11g, outbound network calls are blocked by default. A DBA must grant the database user permission to connect to the target host using an access control entry:
BEGIN
DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE(
host => 'api.example.com',
lower_port => 443,
upper_port => 443,
ace => xs$ace_type(
privilege_list => xs$name_list('http'),
principal_name => 'APP_USER',
principal_type => xs_acl.ptype_db
)
);
END;
/Without this step, calls fail with the error ORA-24247: network access denied by access control list.
Step 2: Configure an Oracle Wallet for HTTPS
Most web services use HTTPS. Oracle needs the service's root and intermediate certificates stored in an Oracle Wallet to establish trusted connections. Use the orapki utility or Oracle Wallet Manager to create a wallet and import the certificates, then reference the wallet path in your PL/SQL code. On Oracle Autonomous Database, common public certificates are already trusted, which simplifies setup.
Step 3: Call a REST Service With UTL_HTTP
UTL_HTTP is a low-level package available in all Oracle editions. Here is an example GET request:
DECLARE
l_req UTL_HTTP.req;
l_resp UTL_HTTP.resp;
l_text VARCHAR2(32767);
l_body CLOB;
BEGIN
UTL_HTTP.set_wallet('file:/u01/app/oracle/wallet', 'wallet_password');
l_req := UTL_HTTP.begin_request(
url => 'https://api.example.com/rates?base=USD',
method => 'GET'
);
UTL_HTTP.set_header(l_req, 'Accept', 'application/json');
UTL_HTTP.set_header(l_req, 'Authorization', 'Bearer YOUR_TOKEN');
l_resp := UTL_HTTP.get_response(l_req);
DBMS_LOB.createtemporary(l_body, TRUE);
BEGIN
LOOP
UTL_HTTP.read_text(l_resp, l_text, 32767);
DBMS_LOB.writeappend(l_body, LENGTH(l_text), l_text);
END LOOP;
EXCEPTION
WHEN UTL_HTTP.end_of_body THEN
UTL_HTTP.end_response(l_resp);
END;
DBMS_OUTPUT.put_line('Status: ' || l_resp.status_code);
END;
/Reading the response in a loop into a CLOB ensures large payloads are handled correctly.
Step 4: Use APEX_WEB_SERVICE for Simpler Code
If Oracle APEX is installed, the APEX_WEB_SERVICE package dramatically simplifies web service calls:
DECLARE
l_response CLOB;
BEGIN
APEX_WEB_SERVICE.g_request_headers.DELETE;
APEX_WEB_SERVICE.g_request_headers(1).name := 'Content-Type';
APEX_WEB_SERVICE.g_request_headers(1).value := 'application/json';
l_response := APEX_WEB_SERVICE.make_rest_request(
p_url => 'https://api.example.com/orders',
p_http_method => 'POST',
p_body => '{"orderId": 1001, "status": "shipped"}',
p_wallet_path => 'file:/u01/app/oracle/wallet'
);
DBMS_OUTPUT.put_line(APEX_WEB_SERVICE.g_status_code);
END;
/APEX_WEB_SERVICE also supports credential stores, OAuth client credentials, and SOAP requests through make_request.
Step 5: Call a SOAP Web Service
To call a SOAP service, build the XML envelope as a string or CLOB, set the Content-Type header to text/xml and the SOAPAction header, and send it with an HTTP POST. APEX_WEB_SERVICE.make_request returns the response as XMLTYPE, which you can query using XPath expressions or XMLTABLE to extract values.
Step 6: Parse JSON Responses
Oracle 12c and later include native JSON support. Use JSON_VALUE for single values, JSON_TABLE to convert arrays into relational rows, or the PL/SQL JSON_OBJECT_T types for programmatic access:
SELECT jt.currency, jt.rate
FROM JSON_TABLE(
l_body, '$.rates[*]'
COLUMNS (
currency VARCHAR2(3) PATH '$.code',
rate NUMBER PATH '$.value'
)
) jt;Older versions can use APEX_JSON for parsing.
Step 7: Handle Errors and Timeouts
Always check the HTTP status code before processing the response. Set timeouts with UTL_HTTP.set_transfer_timeout so long-running calls do not block database sessions. Wrap calls in exception handlers, log failures to an error table with the request details, and make sure responses are always closed to avoid exhausting connections.
Best Practices for Production
- Store credentials in secure credential stores rather than hardcoding them.
- Avoid calling external services inside high-volume triggers or transactions that hold locks.
- Use scheduled jobs with DBMS_SCHEDULER for batch synchronization.
- Cache results in tables when data does not change frequently.
- Monitor call duration and failures to detect issues early.
Troubleshooting Common Errors
A few errors appear again and again when calling web services from PL/SQL. ORA-24247 means the network ACL is missing or does not cover the host and port. ORA-29024 indicates a certificate validation failure, usually because the wallet lacks the correct root or intermediate certificate. ORA-29273 is a generic HTTP request failure, so inspect the underlying error stack for details. Testing the same request with cURL from the database server helps separate network problems from code problems.
Conclusion
Calling a web service in Oracle PL/SQL is entirely achievable once network ACLs and wallets are configured. UTL_HTTP offers full control, while APEX_WEB_SERVICE provides a simpler, higher-level interface. Combine these tools with Oracle's JSON and XML capabilities, robust error handling, and secure credential management to build reliable database-driven integrations.
