I am looking for a example that passes a json file in the request body to the apex_web_service.make_rest_request
Can someone please point me to an example ?
Thanks,
RD
I see two ways to send files via apex_web_service.make_rest_request. In my examples I used an image file blob retrieved from table SAMPLE_TABLE column BLOB_LOGO.
option 1:
cache the file blob type directly from the query into a blob object and send it via p_body_blob parameter
DECLARE
obj_sample SAMPLE_TABLE%ROWTYPE;
file_blob blob;
l_response clob;
BEGIN
-- query
select * INTO obj_sample
from SAMPLE_TABLE where ID = 123;
-- file as blob
file_blob := obj_sample.BLOB_LOGO;
l_response := apex_web_service.make_rest_request(
p_url => 'url/to/api/',
p_http_method => 'POST',
p_body_blob => file_blob,
p_proxy_override => 'url/to/proxy'
);
dbms_output.put_line(l_response);
END;
option 2:
first convert the file blob datatype into a base64 clob object and send it as json body (clob) via p_body parameter
DECLARE
obj_sample SAMPLE_TABLE%ROWTYPE;
json_sample clob;
file_clob clob;
l_response clob;
BEGIN
-- query
select * INTO obj_sample
from SAMPLE_TABLE where ID = 123;
-- file as clob
file_clob := apex_web_service.blob2clobbase64(obj_sample.BLOB_LOGO);
json_sample := json_object(
'SAMPLE_LOGO' value img_clob
);
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 => 'url/to/api/',
p_http_method => 'POST',
p_body => json_sample,
p_proxy_override => 'url/to/proxy'
);
dbms_output.put_line(l_response);
END;
docs
Related
I can upload objects using the following code. How do I upload an object where there is a prefix? Do I need to create the prefix before uploading an object, or will the prefix be created if need be?
declare
l_request_url varchar2(32767);
l_content_length number;
l_response clob;
upload_failed_exception exception;
l_request_object blob;
l_request_filename varchar2(500);
begin
select blob_content, filename into l_request_object, l_request_filename from apex_application_temp_files where name = :P2_FILE;
l_request_url := :G_BASE_URL || 'b/' || :P2_BUCKET_NAME || '/o/' || apex_util.url_encode(l_request_filename);
l_response := apex_web_service.make_rest_request(
p_url => l_request_url
, p_http_method => 'PUT'
, p_body_blob => l_request_object
, p_credential_static_id => 'OCI API Access'
);
end;
Thanks
For upload and delete functions, the prefix is considered to be part of the object name.
If your prefix is aabb and the object is test.png, then the the object name would
aabb/test.png
The only time prefix is used is when listing objects. When you use the prefix, only objects with the same prefix are listed.
I am trying to perform DocuSign Authorization Code Grant via Oracle PL/SQL but I get a 400 Bad Request. It works when I run it via POSTMAN. The headers are correct (For example, I verfified that the authorization code is converted to 64 base correctly. but the POST results I get only 400 Bad request with no reason.
The PL/SQL code is as follows:
FUNCTION obtain_access_token (
p_auth_code in varchar2,
p_integration_key in varchar2,
p_secret_key in varchar2,
p_header1 out varchar2,
p_header2 out varchar2,
p_obtain_auth_token out varchar2
) RETURN CLOB as
l_authorization VARCHAR2(4000);
l_auth_code varchar2(4000);
l_resp clob;
l_wallet_path varchar2(1000):='file:D:\oracle19c\owm\wallets\oracle\wallet';
l_wallet_pwd varchar2(100) := '*******';
l_keys varchar2(4000);
l_encode64_keys varchar2(32767);
l_body clob := 'grant_type=authorization_code&code=#AUTH_CODE#';
l_url varchar2(100) := 'https://account-d.docusign.com/oauth/token';
l_json apex_json.t_values;
BEGIN
l_keys := p_integration_key ||':'||p_secret_key;
l_encode64_keys := DOCUSIGN_PkG.convert_to_base64 (p_key=>l_keys);
l_authorization := rtrim('Basic ' || l_encode64_keys);
--
-- Set headers :
-- Authorization
-- Content-type
--
apex_web_service.g_request_headers(1).name := 'Authorization';
apex_web_service.g_request_headers(1).value := l_authorization ;
apex_web_service.g_request_headers(2).name := 'Content-Type';
apex_web_service.g_request_headers(2).value := 'application/x-www-form-urlencoded';
p_header1 := '{'|| 'Authorization'||','||l_authorization||'}';
p_header2 := '{'|| 'Content-Type'||','||'application/x-www-form-urlencoded'||'}';
--
-- Authorization code
--
l_auth_code := p_auth_code;
-- remove &code=
l_auth_code := substr(l_auth_code,7);
l_body := rtrim('grant_type=authorization_code&code=' ||rtrim(l_auth_code))
--
-- Issue POST
--
l_resp := apex_web_service.make_rest_request (
p_url => l_url,
p_http_method => 'POST',
p_body => l_body,
p_wallet_path => l_wallet_path,
p_wallet_pwd => l_wallet_pwd
);
p_obtain_auth_token:= l_body;
return l_resp;
END obtain_access_token;
I am getting this error:
Error: SyntaxError: JSON.parse Error: Invalid character at position:1
on executing the below code in oracle apex5.2 during submit page,
below code is for downloading a file using pl/sql in oracle apex.
I created a button; when we click that button the below code will execute and submit page also will happen.
Declare
dest_loc11 BLOB := empty_blob();
dest_loc BLOB := empty_blob();
dest_loc2 BLOB := empty_blob();
dest_loc3 BLOB := empty_blob();
dest_loc4 BLOB := empty_blob();
dest_loc5 BLOB := empty_blob();
dest_loc6 BLOB := empty_blob();
src_loc BLOB := empty_blob();
l_zip_file blob;
v_length integer;
v_inp varchar2(32767);
v_count number;
n number:=1;
v_pr1_idx number;
v_pr1_idx2 number;
V_ID NUMBER;
v_pr_1 varchar2(32767):='130614';
V_RES VARCHAR2(32767);
V_PRINT VARCHAR2(32767):='';
V_PROMPT VARCHAR2(32767);
V_PRINT_RAW BLOB;
csv_file utl_file.file_type;
V_NO_DATA varchar2(32767);
v_pr_11 varchar2(32767);
BEGIN
DBMS_LOB.CREATETEMPORARY(
lob_loc => dest_loc11,
cache => true,
dur => dbms_lob.session
);
DBMS_LOB.OPEN(dest_loc11, DBMS_LOB.LOB_READWRITE);
V_PRINT:='udfhsdhgfszhduhjsdzvcjhzxjcvhzxc';
V_PRINT_RAW := utl_raw.cast_to_raw( V_PRINT );
v_length := dbms_lob.getlength(V_PRINT_RAW);
DBMS_LOB.WRITEAPPEND (
lob_loc => dest_loc11,
amount => v_length,
buffer => V_PRINT_RAW);
DBMS_LOB.CLOSE(dest_loc11);
sys.htp.init;
sys.owa_util.mime_header( 'text/plain', FALSE );
sys.htp.p('Content-length: ' || sys.dbms_lob.getlength( dest_loc11));
sys.htp.p('Content-Disposition: attachment; filename="' ||'bala.sql' || '"' );
sys.htp.p('Cache-Control: max-age=3600');-- tell the browser to cache for one hour, adjust as necessary
sys.owa_util.http_header_close;
sys.wpg_docload.download_file( dest_loc11);
DBMS_LOB.FREETEMPORARY (dest_loc11);
apex_application.stop_apex_engine;
end;
You need to set Reload on Submit to Always at page level in the page designer. Then you can successfully download the file. Also your code has a chance of raising Value error when v_length is less than 1 . Make sure you eliminate appending the text to the blob when v_length is outside the limits 1-32767
It sounds like you have a PL/SQL Dynamic Action that fires when the button is clicked, then that downloads your file? Nope, you can't download a file via Ajax.
Download a file by jQuery.Ajax
What you need to do is redirect the user to a different page, or back to the current page with a different REQUEST value, so you know you're supposed to be downloading the file. Then, put your code in a Before Header process on that page.
Say i have two files with the same filename sample.xlsx
in two separate directories u1\data\out1\ and u2\data\out2\
both of these directories already have entried in DBA_DIRECTORIES as EXT_OUT1 and EXT_OUT2, respectively.
I would like to send sample.xlsx from EXT_OUT1 using UTL_MAIL.SEND_ATTACH_RAW,
how can i pass it correctly to the attachment parameter?
Sample Anonymous Block (Note the comments):
DECLARE
vInHandle utl_file.file_type;
l_sender varchar2(100) := 'SO#SO.com';
l_recipients varchar2(100) := 'migs.isip.23#gmail.com';
l_subject varchar2(100) := 'Employee Roster Report';
l_message varchar2(100) := 'Hello';
l_attachment raw;
l_directory varchar2(100) := 'EXT_OUT1';
fname varchar2(100) := 'sample.xlsx';
BEGIN
/* how put RAW data into l_attachment here? */
--vInHandle := utl_file.fopen(l_directory, fname, 'R'); -- If i'm not mistaken, this reads the File from the specified directory
--utl_file.get_raw(); -- not sure what parameters i should pass
--utl_file.fclose(vInHandle); -- ?
UTL_MAIL.SEND_ATTACH_RAW
(
sender => l_sender
, recipients => l_recipients
, subject => l_subject
, message => l_message
, attachment => l_attachment
, att_filename => 'clouds.jpg'
);
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'The following error has occured: ' || sqlerrm);
END;
The attachment content is the value you are supplying as the attachment parameter. The filename is simply a label. It doesn't read the file, so you'd have to do that yourself and then assign that as the attachment.
Can someone please share the list of steps to be followed or a sample code to attach a simple csv or Xls file from utl file directory and send it through email using utl smtp package.
Try this one to read a text file from file system:
FUNCTION GetFile(FolderName IN VARCHAR2, FileName IN VARCHAR2) RETURN CLOB IS
v_bfile BFILE;
v_clob CLOB;
destOffset INTEGER := 1;
srcOffset INTEGER := 1;
lang_context INTEGER := DBMS_LOB.default_lang_ctx;
warning INTEGER;
BEGIN
v_bfile := BFILENAME (FolderName, FileName);
DBMS_LOB.OPEN (v_bfile);
DBMS_LOB.CREATETEMPORARY(v_clob, TRUE, DBMS_LOB.SESSION);
DBMS_LOB.LOADCLOBFROMFILE(
dest_lob => v_clob,
src_bfile => v_bfile,
amount => DBMS_LOB.GETLENGTH(v_bfile),
dest_offset => destOffset,
src_offset => srcOffset,
bfile_csid => DBMS_LOB.default_csid,
lang_context => lang_context,
warning => warning);
DBMS_LOB.CLOSE(v_bfile);
RETURN v_clob;
END GetFile;
After you read the file into CLOB you can pass it to this procedure: How to send email using Oracle 10 g Forms