Testing REST calls is a crucial step in developing and maintaining RESTful services in Oracle APEX. It ensures that your APIs function correctly, return the expected data, and handle errors gracefully. By thoroughly testing REST calls, you can verify the integrity, security, and performance of your web services before they are used in production environments. This blog will guide you through practical methods and tools for effectively testing REST calls in Oracle APEX.
Testing REST calls in Oracle APEX is an essential step to ensure that your RESTful services are working as expected. To test a REST call, you can use built-in APEX tools or external tools such as Postman or curl.
First, in Oracle APEX, you can navigate to Shared Components > RESTful Services. Select the RESTful Service module or specific REST endpoint you want to test. APEX provides a “Test” option where you can enter parameters if required and execute the REST call. The response, including the status code, headers, and body content, will be displayed in a readable format. This allows you to verify that the service returns the correct data and status.
If you prefer using external tools, Postman is highly recommended. With Postman, you can craft GET, POST, PUT, DELETE, or other HTTP requests to your REST endpoints. You set the URL, HTTP method, headers, and payload as needed. Sending the request shows you detailed response information, including the status, headers, and response body. This is useful for testing complex requests or simulating client interactions.
Alternatively, command-line tools like curl allow quick testing of REST endpoints from a terminal. For example, a simple curl command can send a GET request and display the response directly.
When testing REST calls, pay close attention to HTTP status codes such as 200 for success, 400 for bad requests, or 500 for server errors. Also, verify that response data matches expected formats, such as JSON or XML, and that any authentication or authorization requirements are correctly handled.
By thoroughly testing REST calls, you can identify and resolve issues early, ensure your APIs behave as intended, and provide a stable and secure integration within your Oracle APEX applications.
To test your RESTful service call:
Enter an employee ID in the P1_EMPLOYEE_ID text box.
Trigger the process (e.g., by clicking a button).
Check the output, either in the page item or via the debug output in Oracle APEX.
Step by Step simplest method “Example”
Continuing from the previous section, create a “Module”
Enter the required fields.
Should look something like this:
Go back to the newly created Module
Lets add a handler
This handler will look for the id and return those rows. Add the SQL code below.
Navigating back to the module and slecteting the ID template displays the following
The new “ID” handler should look lik the following two images.
The new handler will display at the bottom of the screen.
To test the call the Service use the following:
adbnphxadcdev01.adb.us-phoenix-1.oraclecloudapps.com/ords/xxdii_paul_test/airportslist/1
That will return the following from a browser:
Or the text equivalent of:
{"items":[{"id":1,"code":"0AK","name":"Pilot Station Airport","city":"Pilot Station","state":"AK"}],"hasMore":false,"limit":25,"offset":0,"count":1,"links":[{"rel":"self","href":"https://adbnphxadcdev01.adb.us-phoenix-1.oraclecloudapps.com/ords/xxdii_paul_test/airportslist/1"},{"rel":"describedby","href":"https://adbnphxadcdev01.adb.us-phoenix-1.oraclecloudapps.com/ords/xxdii_paul_test/metadata-catalog/airportslist/item"},{"rel":"first","href":"https://adbnphxadcdev01.adb.us-phoenix-1.oraclecloudapps.com/ords/xxdii_paul_test/airportslist/1"}]}
Common Scenarios for REST Synchronization:
Fetching External Data:
You can set up REST synchronization to regularly pull data from an external service (such as a weather API, earthquake data, or social media API) and display it in your APEX application.
Sending Data to External Services:
You can push data from your APEX forms to external systems (e.g., submitting user data to a CRM or payment gateway).
Periodic Synchronization:
Schedule periodic synchronization (e.g., every hour or day) to fetch or send data automatically without user intervention.
REST Synchronization in Oracle APEX: How It Works
Oracle APEX provides multiple ways to synchronize data with external RESTful services:
Web Source Modules:
Web Source modules allow you to connect APEX to RESTful APIs and create Read-only data sources. You can use a Web Source to pull data from the external API and display it in your APEX reports or forms.
Example: If you want to display earthquake data from the USGS API in an APEX report, you would define a Web Source for the API endpoint and map the fields.
Steps to create a Web Source:
Create a Web Source in your APEX application.
Specify the REST API endpoint and request method (GET, POST, etc.).
Define how to parse the JSON or XML response and map it to APEX application columns.
RESTful Web Services in APEX:
APEX allows you to expose RESTful Web Services that can be consumed by external applications or APIs. This is typically used when you want APEX to push data to another system via REST.
APEX provides RESTful Web Service endpoints, and you can define these services to accept or return data in JSON or XML format.
EXAMPLE:
APEX exposes a REST endpoint that an external system can send a POST request to, containing data that will be inserted into your APEX application's database.
Interactive Grid with REST Integration:
You can use an Interactive Grid in APEX to integrate with REST APIs for CRUD operations (Create, Read, Update, Delete). The grid allows users to interact with remote data by making REST calls when records are added, edited, or deleted.
Scheduled Jobs for Periodic Synchronization:
You can set up scheduled jobs in APEX (or Oracle DB) to periodically fetch or push data to external APIs. This can be useful for synchronizing data at regular intervals, without requiring manual intervention.
Using PL/SQL to Interact with REST APIs:
For more custom control over the synchronization process, you can use PL/SQL to interact with RESTful APIs. The UTL_HTTP package in PL/SQL allows you to make HTTP requests to external REST services. You can use this to fetch data and then insert/update it in APEX.
In conclusion, testing REST calls is essential for delivering reliable and robust web services within Oracle APEX. By using tools such as Postman, curl, or built-in testing features, you can validate your API endpoints, troubleshoot issues, and improve the overall user experience. Regular and comprehensive testing helps prevent unexpected failures and ensures that your RESTful services meet both functional and security requirements.