I am trying to call REST API using Oracle APEX provided function apex_web_service.make_rest_request by passing JSON.
JSON is created by using below utility by passing the sys_refcursor.
apex_json.open_object;
apex_json.write(l_sys_refcursor);
apex_json.close_object;
lclob_body := apex_json.get_clob_output;
Now I am passing the value of lclob_body to below
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
);
This is working fine for single JSON but when sys_refcursor returns multiple rows then multiple json is getting created. In this case, only first json is passing to API call.
How to pass each json (for each row returned by sys_refcursor) one by one to the function make_rest_request to call the API?
Edit : 1
I have done this simply by iterating the sys_refcursor into variables.
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;
With this approach, able to call API for each json. But not sure if this is right way to achieve this or not. Please suggest if there is any other better way to achieve this.
Thank You!