ABAP 调用HTTP上传附件

1、需求说明在SAP中调用第三方文件服务器的HTTP请求,将文件保存在文件服务器上,并返回保存的文件地址 。SAP保存返回的文件地址,通过浏览器进行访问 。
2、需求实现2.1、POSTMAN测试通过POSTMAN测试成功,获取返回的文件地址

ABAP 调用HTTP上传附件

文章插图
并且可以看到请求对应编程语言的示例代码
ABAP 调用HTTP上传附件

文章插图
2.2、代码示例通过文件服务器URL创建http_client,设置类型为multipart/form-data,并传输请求参数
"-----------------------------@斌将军-----------------------------TYPES: BEGIN OF ty_itab,raw(255) TYPE x,END OF ty_itab.DATA:lt_bin TYPE STANDARD TABLE OF ty_itab,ls_bin LIKE LINE OF lt_bin.DATA: lv_lenTYPE i, "发送报文长度lv_urlTYPE string, "接口地址lo_http_client TYPE REF TO if_http_client, "http客户端lo_partTYPE REF TO if_http_entity,lv_valueTYPE string,lv_nameTYPE string,lv_fileTYPE string,lv_rec_jsonTYPE string, "接收的JSONlv_codeTYPE i, "HTTP 返回状态lv_reasonTYPE string. " HTTP 状态描述DATA:lv_filelength TYPEi,lv_xstringTYPE xstring.DATA:lv_account TYPE string,lv_passTYPE string. 。。。。。。"创建http客户端CALL METHOD cl_http_client=>create_by_urlEXPORTINGurl= lv_urlIMPORTINGclient= lo_http_clientEXCEPTIONSargument_not_found= 1plugin_not_active= 2internal_error= 3pse_not_found= 4pse_not_distrib= 5pse_errors= 6oa2c_set_token_error= 7oa2c_missing_authorization = 8oa2c_invalid_config= 9oa2c_invalid_parameters= 10oa2c_invalid_scope= 11oa2c_invalid_grant= 12OTHERS= 13.IF sy-subrc <> 0.e_return-type = 'E'.e_return-message = '无法创建http client'.RAISE error.ENDIF."设置http method 为POSTCALL METHOD lo_http_client->request->set_header_fieldEXPORTINGname= '~request_method'value = 'https://www.huyubaike.com/biancheng/POST'."SET protocol versionCALL METHOD lo_http_client->request->set_version(if_http_request=>co_protocol_version_1_1 )."content typeCALL METHOD lo_http_client->request->set_content_typeEXPORTINGcontent_type = 'multipart/form-data'."用户名CALL METHOD lo_http_client->request->set_form_fieldEXPORTINGname= 'accessKey'value = https://www.huyubaike.com/biancheng/lv_account."文件夹名称CALL METHOD lo_http_client->request->set_form_fieldEXPORTINGname= 'bucketName'value = 'https://www.huyubaike.com/biancheng/sap'."密码CALL METHOD lo_http_client->request->set_form_fieldEXPORTINGname= 'secretKey'value = https://www.huyubaike.com/biancheng/lv_pass.""-----------------------------@斌将军-----------------------------浏览器对应的编码和语言设置,可以参考浏览器中测试时,真实发送的请求参数
ABAP 调用HTTP上传附件

文章插图
"-----------------------------@斌将军-----------------------------"浏览器支持的 MIME 类型CALL METHOD lo_http_client->request->set_header_fieldEXPORTINGname= 'Accept'value = 'https://www.huyubaike.com/biancheng/*/*'."浏览器支持的压缩编码CALL METHOD lo_http_client->request->set_header_fieldEXPORTINGname= 'Accept-Encoding'value = 'https://www.huyubaike.com/biancheng/gzip, deflate'."浏览器支持的语言CALL METHOD lo_http_client->request->set_header_fieldEXPORTINGname= 'Accept-Language'value = 'https://www.huyubaike.com/biancheng/zh-CN,zh;q=0.9,de;q=0.8,en;q=0.7'."-----------------------------@斌将军-----------------------------添加上传的文件,其中文件名需要进行URL编码,可避免文件的中文乱码
其中name="file" 对应POSTMAN请求参数中的file
"-----------------------------@斌将军-----------------------------CALL METHOD lo_http_client->request->if_http_entity~set_formfield_encodingEXPORTINGformfield_encoding = cl_http_request=>if_http_entity~co_encoding_raw.lo_part = lo_http_client->request->if_http_entity~add_multipart( )."拼接上传的文件名,并将文件名转码lv_name = i_filename.lv_name = cl_http_utility=>escape_url( lv_name ).lv_value = 'https://www.huyubaike.com/biancheng/form-data; name="file"; filename="' && lv_name && '";'.CALL METHOD lo_part->set_header_fieldEXPORTINGname= 'content-disposition'*value = 'https://www.huyubaike.com/biancheng/form-data; name="file"; filename="11.txt";'.value = https://www.huyubaike.com/biancheng/lv_value.CALL METHOD lo_part->set_content_typeEXPORTINGcontent_type ='application/x-www-form-urlencoded'."-----------------------------@斌将军-----------------------------上传本地文件为二进制,并转为xstring格式
"-----------------------------@斌将军-----------------------------CALL FUNCTION 'GUI_UPLOAD'EXPORTINGfilename= lv_filefiletype= 'BIN'IMPORTINGfilelength= lv_filelengthTABLESdata_tab= lt_binEXCEPTIONSfile_open_error= 1file_read_error= 2no_batch= 3gui_refuse_filetransfer = 4invalid_type= 5no_authority= 6unknown_error= 7bad_data_format= 8header_not_allowed= 9separator_not_allowed= 10header_too_long= 11unknown_dp_error= 12access_denied= 13dp_out_of_memory= 14disk_full= 15dp_timeout= 16OTHERS= 17.IF sy-subrc <> 0.e_return-type = 'E'.e_return-message = '读取文件失败'.RAISE error.ENDIF.CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'EXPORTINGinput_length = lv_filelengthIMPORTINGbuffer= lv_xstringTABLESbinary_tab= lt_binEXCEPTIONSfailed= 1OTHERS= 2."-----------------------------@斌将军-----------------------------

推荐阅读