Search code examples
oracle-databaserestoracle-apex

ORACLE APEX web service p_body_blob


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


Solution

  • 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