13.5.3 Calling REST APIs Without a Data Source
Use the APEX_WEB_SERVICE package to call a REST API that
requires a binary payload or returns a binary response. Binary payloads require this
approach. You can also use the package for general REST calls, but then you must code many
tasks that a REST Data Source handles declaratively.
For more information, see MAKE_REST_REQUEST Function in Oracle APEX API Reference.
Tip:
To pass a binary request payload, use p_body_blob instead of p_body. To receive a binary response, use MAKE_REST_REQUEST_B instead of MAKE_REST_REQUEST.
For easier comparison, the example below calls the same Single Book Order endpoint without using a REST Data Source.
declare
c_endpoint_url constant varchar2(200) :=
'https://example.com/ords/cloudcompanion/orders/singleBookCreate';
l_new_order_number number;
l_estimated_delivery date;
l_request json_object_t;
l_response_payload clob;
l_response json_object_t;
begin
-- Build the request body JSON
l_request := json_object_t();
l_request.put('isbn','978‑1565926912');
l_request.put('quantity',1);
-- POST the JSON request body to the REST API endpoint URL
l_response_payload :=
apex_web_service.make_rest_request(
p_http_method => 'POST',
p_url => c_endpoint_url,
p_body => l_request.to_clob,
p_credential_static_id => null /* Non-null for auth case */,
p_token_url => null /* Non-null for OAuth */);
-- If HTTP status code indicates a success and response is not empty
if apex_web_service.g_status_code = 200
and l_response_payload is not null
and dbms_lob.getlength(l_response_payload) > 0
then
-- Work with the response body as JSON
l_response := json_object_t(l_response_payload);
-- Reference the values from the response JSON
l_new_order_number := l_response.get_number('orderNumber');
l_estimated_delivery := l_response.get_date('estimatedDelivery');
:P55_SUCCESS_MESSAGE := apex_string.format(
'New Order %s should deliver %s',
l_new_order_number,
to_char(l_estimated_delivery,
'DD-MON-YYYY'));
end if;
end;Parent topic: Using REST APIs in Code