0 Replies Latest reply on Oct 7, 2014 12:49 PM by 2769156

    APEX- APEX_WEB_SERVICES.MAKE_REST_REQUEST for POST - attachment of files

    2769156

      I am facing problem in uploading a file/ file attachments using RESTful API's. I am aware of APEX_WEB_SERVICES.MAKE_REST_REQUEST,  but its giving me errors. If any1 has earlier used it, plz post an example or explain how it works..

      My approach code:

       

      declare

        l_clob varchar2(32767);

        l_in clob;

        l_blob blob;

        secure varchar2(100);

      BEGIN

       

      /*----------Setting Headers----------------------------------------*/                                     

                     apex_web_service.g_request_headers(1).name := 'Accept';

                     apex_web_service.g_request_headers(1).Value := 'multipart/form-data';

              apex_web_service.g_request_headers(1).name := 'Content-Type';

              apex_web_service.g_request_headers(1).value := 'multipart/form-data';

      /*-----------------------------------------------------------------*/

       

      /*------------Calling Authentication Rest service------------------*/

                                                   l_clob := apex_web_service.make_rest_request(

                                                                                                p_url => 'My url to authenticate'

                                                                                                p_http_method => 'POST',

                                                                                                p_username => 'XXXXXX',

                                                                                                p_password => '######',

                                                                                                p_wallet_path => 'file:C:\Users\',

                                                                                                p_wallet_pwd => '5162);

                                                  

      /*------------- Retrieving Cookies---------------------------------*/

         apex_collection.create_or_truncate_collection('P31_RESP_COOKIES');

        for i in 1.. apex_web_service.g_response_cookies.count loop

          IF (apex_web_service.g_response_cookies(i).secure) THEN

            secure := 'Y';

          ELSE

            secure := 'N';

          END IF;

          apex_collection.add_member(p_collection_name => 'P31_RESP_COOKIES',

            p_c001 => apex_web_service.g_response_cookies(i).name,

            p_c002 => apex_web_service.g_response_cookies(i).value,

            p_c003 => apex_web_service.g_response_cookies(i).domain,

            p_c004 => apex_web_service.g_response_cookies(i).expire,

            p_c005 => apex_web_service.g_response_cookies(i).path,

            p_c006 => secure,

            p_c007 => apex_web_service.g_response_cookies(i).version );

        end loop;

      /*------------------------------------------------------------------*/

       

      /*------------- Setting Cookies-------------------------------------*/

          for c1 in (select seq_id, c001, c002, c003, c004, c005, c006, c007

                   from apex_collections

                  where collection_name = 'P31_RESP_COOKIES' ) loop

         apex_web_service.g_request_cookies(c1.seq_id).name := c1.c001;

         apex_web_service.g_request_cookies(c1.seq_id).value := c1.c002;

         apex_web_service.g_request_cookies(c1.seq_id).domain := c1.c003;

         apex_web_service.g_request_cookies(c1.seq_id).expire := c1.c004;

         apex_web_service.g_request_cookies(c1.seq_id).path := c1.c005;

         if c1.c006 = 'Y' then

           apex_web_service.g_request_cookies(c1.seq_id).secure := true;

         else

           apex_web_service.g_request_cookies(c1.seq_id).secure := false;

         end if;

           apex_web_service.g_request_cookies(c1.seq_id).version := c1.c007;

      end loop;

       

      select test_data into l_blob from my_table; -- gets blob datatype file

       

      l_in := '<Entity Type="attachments">

                  <Fields>                             

                      <Field Name="filename">

                          <Value>sample case</Value>

                      </Field>

                      <Field Name="description">

                          <Value></Value>

                      </Field> 

                      <Field Name="override-existing-attachment">

                          <Value>n<value/>

                      </Field>

                      <Field Name="ref-subtype">

                          <Value>0</Value>

                      </Field>

                      <Field Name="data">

                          <Value>';

      l_in := l_in||apex_web_service.blob2clobbase64(l_blob);

      dbms_output.put_line(l_in);

      l_in := l_in||'</Value>

                      </Field>

                  </Fields>

              </Entity>';

      /*------------------------------------------------------------------*/

              apex_web_service.g_request_headers(1).name := 'Accept';

                     apex_web_service.g_request_headers(1).Value := 'multipart/form-data';

              apex_web_service.g_request_headers(1).name := 'Content-Type';

              apex_web_service.g_request_headers(1).value := 'multipart/form-data';

      /*-- not sure with the following code--*/

      l_clob := apex_web_service.make_rest_request(p_url => 'myurl/tests/13142/attachments',

                                                                                                p_http_method => 'POST',

                                                                                                p_body = l_in,

                                                                                                p_body_blob => l_blob,  

                                                                                                p_wallet_path => 'same as above',

                                                                                                p_wallet_pwd => 'same as above');

                    

         

      end;