Her giver jeg et eksempel på at sende JSON og en zip-fil til REST
webtjeneste ved hjælp af Oracle UTL_HTTP
post multipart/form-data.
Til at begynde med tog jeg eksempelkoden fra Nick Buytaerts blog og ændrede den derefter til at inkorporere JSON
og zip-filen.
Send JSON og en zip-fil til REST-webtjeneste ved hjælp af UTL_HTTP
Den følgende PL/SQL-kode vil udføre REST
webservicegodkendelse ved hjælp af token. Og derefter vil den få JSON
og KLAPPEN fra bordet. Det vil zippe BLOB
og kombiner det derefter med JSON
ved at konvertere den til base64
type data. Derefter forbereder den headeren og sender den.
declare l_attachment blob; l_newline varchar2(50) := chr(13) || chr(10); lco_boundary constant varchar2(30) := 'gc0p4Jq0M2Yt08jU534c0p'; l_http_request utl_http.req; l_request_body clob; l_request_body_length number; l_req_body clob; l_http_response utl_http.resp; l_response_header_name varchar2(256); l_response_header_value varchar2(1024); l_response_body varchar2(32767); l_offset number := 1; l_amount number := 2000; l_buffer varchar2(2000); l_clob clob; l_token varchar2(32767); begin -- get the token l_clob := apex_web_service.make_rest_request( p_url => 'https://YourTokenURL', p_http_method => 'GET'); l_status := apex_web_service.g_status_code; APEX_JSON.parse(l_clob); l_token:=APEX_JSON.get_varchar2(p_path => 'token'); -- prepare or get the json l_req_body := '{name: "Scott", age: 33, city: "Huston"}'; l_request_body := l_newline || '--' || lco_boundary || l_newline || 'Content-Disposition: form-data; name="contact-info"' || l_newline || 'Content-Type: application/json' || l_newline || l_newline || l_req_body; --|| l_newline --|| '--' || lco_boundary || '--'; -- get the blob from the table and zip it FOR l_file IN ( SELECT file_name, my_file_blob FROM my_doc_table WHERE doc_id = '1234' ) LOOP apex_zip.add_file(p_zipped_blob => l_attachment, p_file_name => l_file.file_name, p_content => l_file.my_file_blob); END LOOP; apex_zip.finish(p_zipped_blob => l_attachment); -- concatenate zip file as base64 data to the above json l_request_body := l_request_body || l_newline || '--' || lco_boundary || l_newline || 'Content-Disposition: form-data; name="attachment"' || l_newline || 'Content-Type: application/zip' || l_newline || 'Content-Transfer-Encoding: base64' || l_newline || l_newline || apex_web_service.blob2clobbase64(l_attachment) || l_newline || '--' || lco_boundary || '--'; dbms_output.put_line('Request body>'); dbms_output.put_line(dbms_lob.substr(l_request_body, 4000, 1)); l_request_body_length := dbms_lob.getlength(l_request_body); -- authenticate wallet utl_http.set_wallet( path => 'file:/your/wallet/path', password => 'YourWalletPsw' ); -- start sending the data l_http_request := utl_http.begin_request( url => 'https://yourRESTservicePostURL', method => 'POST', http_version => 'HTTP/1.1' ); -- set header utl_http.set_header(l_http_request, 'Authorization', 'Bearer ' || l_token); utl_http.set_header(l_http_request, 'Content-Type', 'multipart/form-data; boundary="' || lco_boundary || '"'); utl_http.set_header(l_http_request, 'Content-Length', l_request_body_length); utl_http.set_header(l_http_request, 'Transfer-Encoding', 'Chunked'); utl_http.set_header(l_http_request, 'Connection', 'keep-alive'); -- send data in chunks while l_offset < l_request_body_length loop dbms_lob.read(l_request_body, l_amount, l_offset, l_buffer); utl_http.write_text(l_http_request, l_buffer); l_offset := l_offset + l_amount; end loop; -- print the response l_http_response := utl_http.get_response(l_http_request); dbms_output.put_line('Response> Status Code: ' || l_http_response.status_code); dbms_output.put_line('Response> Reason Phrase: ' || l_http_response.reason_phrase); dbms_output.put_line('Response> HTTP Version: ' || l_http_response.http_version); for i in 1 .. utl_http.get_header_count(l_http_response) loop utl_http.get_header(l_http_response, i, l_response_header_name, l_response_header_value); dbms_output.put_line('Response> ' || l_response_header_name || ': ' || l_response_header_value); end loop; utl_http.read_text(l_http_response, l_response_body, 32767); dbms_output.put_line('Response body>'); dbms_output.put_line(l_response_body); if l_http_request.private_hndl is not null then utl_http.end_request(l_http_request); end if; if l_http_response.private_hndl is not null then utl_http.end_response(l_http_response); end if; exception when others then if l_http_request.private_hndl is not null then utl_http.end_request(l_http_request); end if; if l_http_response.private_hndl is not null then utl_http.end_response(l_http_response); end if; raise; end;
Denne kode fungerer som en charme. Fortæl mig venligst i kommentarfeltet, hvis du har problemer.