Skip to content

Instantly share code, notes, and snippets.

@jon-dixon
Last active December 24, 2022 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jon-dixon/22241c69d2c817929c8f74286765fb57 to your computer and use it in GitHub Desktop.
Save jon-dixon/22241c69d2c817929c8f74286765fb57 to your computer and use it in GitHub Desktop.
ARCS Upload File Procedure
PROCEDURE upload_file
(p_base_url IN VARCHAR2,
p_file_name IN VARCHAR2,
p_file_path IN VARCHAR2,
p_file_blob IN BLOB) IS
l_response_clob CLOB;
l_complete_url VARCHAR2(500);
lc_interop_path CONSTANT VARCHAR2(100) := 'interop/rest/11.1.2.3.600/applicationsnapshots/';
l_status NUMBER;
l_details VARCHAR2(1000);
BEGIN
-- Set the HTTP Headers for uploading a binary file.
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/octet-stream';
-- Build complete URL to REST Service including parameter to set file path. URL Encode the filename.
l_complete_url := p_base_url || lc_interop_path ||
apex_util.url_encode(p_url => p_file_name) || '/contents'||
CASE WHEN p_file_path IS NULL THEN NULL ELSE '?extDirPath='||p_file_path END;
-- Call REST Service to Upload the File.
l_response_clob := apex_web_service.make_rest_request
(p_url => l_complete_url,
p_http_method => 'POST',
p_transfer_timeout => GC_REST_TIMEOUT_SECS,
p_body_blob => p_file_blob,
p_credential_static_id => 'ARCS_BASIC_AUTH');
-- The Web Credential 'ARCS_BASIC_AUTH' is used to Authentiocate against the ARCS REST Service.
-- apex_web_service tajes care of the everyhting else for us.
IF apex_web_service.g_status_code = 200 THEN
-- Although a HTTP status of 200 indicates success, we need to
-- check the 'status' field in the JSON response to make sure it is 0 also.
apex_json.parse(l_response_clob);
l_status := apex_json.get_number(p_path => 'status');
IF l_status <> 0 THEN
-- If the status field is not 0 then get the error message from the details field.
l_details := apex_json.get_varchar2(p_path => 'details');
-- Raise an Error to Stop processing.
raise_application_error(-20001, 'Failed to Upload File ['||l_details||']');
END IF;
ELSE
-- If we get a HTTP status code other than 200 then Raise an Error to Stop processing.
raise_application_error(-20002, 'Error from Upload File API ['||apex_web_service.g_status_code||']');
END IF;
END upload_file;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment