我正在嘗試使用 Oracle APEX 提供的函式apex_web_service.make_rest_request通過傳遞 JSON 來呼叫 REST API。
JSON 是通過傳遞 sys_refcursor 使用以下實用程式創建的。
apex_json.open_object;
apex_json.write(l_sys_refcursor);
apex_json.close_object;
lclob_body := apex_json.get_clob_output;
現在我將 lclob_body 的值傳遞給下面
begin
apex_web_service.set_request_headers(
p_name_01 => 'Content-Type',
p_value_01 => 'application/json',
p_name_02 => 'User-Agent',
p_value_02 => 'APEX',
p_name_03 => 'Authorization',
p_value_03 => 'Basic xxxasdasdasdsaddsadsdsasfsafa',
p_reset => true,
p_skip_if_exists => true );
end;
v_response := apex_web_service.make_rest_request
(
p_url => 'https://....api_url',
p_http_method => 'POST',
p_body => lclob_body
);
這適用于單個 JSON,但是當 sys_refcursor 回傳多行時,會創建多個 json。在這種情況下,只有第一個 json 傳遞給 API 呼叫。
如何將每個json (針對sys_refcursor回傳的每一行)一個一個傳遞給函式make_rest_request呼叫API?
編輯:1
我只是通過將 sys_refcursor 迭代到變數中來做到這一點。
loop
fetch l_cursor into p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15;
exit when l_cursor%notfound;
apex_json.open_object;
apex_json.write('field1',p1);
apex_json.write('field2',p2);
apex_json.write('field3',p3);
apex_json.write('field4',p4);
apex_json.write('field5',p5);
apex_json.write('field6',p6);
apex_json.write('field7',p7);
apex_json.write('field8',p8);
apex_json.write('field9',p9);
apex_json.write('field10',p10);
apex_json.write('field11',p11);
apex_json.write('field12',p12);
apex_json.write('field13',p13);
apex_json.write('field14',p14);
apex_json.write('field15',p15);
apex_json.close_object;
lclob_body := apex_json.get_clob_output;
begin
apex_web_service.set_request_headers(
p_name_01 => 'Content-Type',
p_value_01 => 'application/json',
p_name_02 => 'User-Agent',
p_value_02 => 'APEX',
p_name_03 => 'Authorization',
p_value_03 => 'Basic xxxasdasdasdsaddsadsdsasfsafa',
p_reset => true,
p_skip_if_exists => true );
end;
v_response := apex_web_service.make_rest_request
(
p_url => 'https://....api_url',
p_http_method => 'POST',
p_body => lclob_body
);
dbms_output.put_line(v_response);
exception when others then
null;
end;
apex_json.free_output;
end loop;
使用這種方法,能夠為每個 json 呼叫 API。但不確定這是否是實作這一目標的正確方法。請建議是否有其他更好的方法來實作這一點。
謝謝你!
uj5u.com熱心網友回復:
好吧,最后你必須遍歷你的游標,并且對于每次迭代,你都會構建一個 JSON 物件,呼叫 APEX_WEB_SERVICE 并存盤結果。
接受游標的 APEX_JSON.WRITE 程序不適合您,因為它(您已經注意到)生成一個包含所有資料的 JSON 物件。但這不是你需要的。
我認為,第二種方法,使用本機 SQL/JSON 函式是最好的解決方案 - 因為它最適合您的要求。并且本機功能通常也比 APEX_JSON 等 PL/SQL 實作更快。
uj5u.com熱心網友回復:
對本機 JSON 功能執行相同的操作,如下所示
set serveroutput on;
declare
v_response clob;
cursor c1 is
SELECT
json_object('field1' value 'AABBCC',
'field2' value t2.col1 ,
'field3' value t1.col1,
'field4' value 'Corp' ,
'field5' value 'AABB'||t2.col2 ,
'field6' value JSON_object (
'nested_field1' value '2029-11-13',
'nested_field2' value 'XYZ' ,
'nested_field3' value 'ABC' ,
'nested_field4' value '2019-11-12',
'nested_field5' value '1234' ,
'nested_field6' value 'ABC'
) ,
'field7' value '' ,
'field8' value 'TEAM' ,
'field9' value 'true' ,
'field10' value '12345' ,
'field11' value 'Scale' ,
'field12' value t2.col3 ,
'field13' value t1.col2 ,
'field14' value 'RUP' ,
'field15' value 'ZZ'
) a
FROM
tab1 t1,
tab2 t2
WHERE
t1.col1 = t2.col2
AND t1.col5 IS NULL;
begin
apex_web_service.set_request_headers(
p_name_01 => 'Content-Type',
p_value_01 => 'application/json',
p_name_02 => 'User-Agent',
p_value_02 => 'APEX',
p_name_03 => 'Authorization',
p_value_03 => 'Basic xxxasdasdasdsaddsadsdsasfsafa',
p_reset => true,
p_skip_if_exists => true );
for i in c1 loop
begin
v_response := apex_web_service.make_rest_request
(
p_url => 'https://....api_url',
p_http_method => 'POST',
p_body => i.a
);
exception when others then
null;
end;
end loop;
end;
/
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/530171.html
下一篇:使用ViewModel更新屬性值
