POST的APEX- APEX_WEB_SERVICES.MAKE_REST_REQUEST - 文件附件

时间:2014-10-07 12:39:36

标签: web-services file-upload attachment apex flask-restful

我在使用RESTful API上传文件/文件附件时遇到问题。我知道APEX_WEB_SERVICES.MAKE_REST_REQUEST,但它给了我错误。如果any1早先使用过它,请发布一个例子或解释它是如何工作的。 我的方法代码:

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;

1 个答案:

答案 0 :(得分:0)

Achieved it as:
/*change dis part in above code*/

select test_data into l_blob from h_attachments;
/*------------------------------------------------------------------*/
        apex_web_service.g_request_headers(1).name := 'Accept';
               apex_web_service.g_request_headers(1).Value := 'application/xml';

        apex_web_service.g_request_headers(1).name := 'Content-Type';
        apex_web_service.g_request_headers(1).value := 'application/octet-stream';

        apex_web_service.g_request_headers(1).name := 'Slug';
        apex_web_service.g_request_headers(1).value := 'filename.xls';

l_clob := apex_web_service.make_rest_request(p_url => 'your url path',
                                             p_http_method => 'POST',
                                             p_body_blob  => l_blob, 
                                             p_wallet_path => 'file:C:\Users\ur path',
                                             p_wallet_pwd => 'passwd');