从ABAP程序调用外部REST API

2020-09-02 02:29发布

点击此处---> 群内免费提供SAP练习系统(在群公告中)加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)专家您好, 我需要使用abap...

         点击此处--->   EasySAP.com群内免费提供SAP练习系统(在群公告中)

加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)


专家您好,

我需要使用abap程序(POST方法)从SAP系统(SAP_BASIS 740 SP-杠杆9)调用外部REST API。

我不知道如何将pdf文件传输到API。 该API的参数为:文件(列表到pdf文件),收件人[名称]和收件人[电子邮件]。

数据:ls_xml TYPE字符串,
ls_respuesta_xml TYPE字符串,
ld_urlurl TYPE字符串,
lo_https_client TYPE REF TO if_http_client,
lo_http_entity TYPE REF TO if_http_entity,
lo_request TYPE REF to if_rest
lo_response类型引用到if_rest_entity,
lv_http_status类型字符串,
lv_status类型字符串,
lv_reason类型字符串,
lv_response类型字符串,
lv_content_length类型字符串,
lv_location类型字符串 ,
lv_content_type TYPE字符串,
lv_body TYPE字符串,
lt_tab_file TYPE CHAR表,
ls_xstring TYPE xstring,
lv_length TYPE i,
lv_string TYPE字符串,
lv_rfc_dest TYPE rfcdest,
lo_http_client TYPE REF到if_http_client,
lo_rest_client TYPE REF到cl_rest_http_client,
lo_request_entity TYPE REF到if_rest_entity,
lt_fields TYPE tihttpnvp,
ls_fields TYPE ihttp。 br>"上载PDF文件

调用方法cl_gui_frontend_services => gui_upload

导出
文件名='Z:\ file.pdf '
文件类型= 'BIN'IMPORTING
文件长度= lv_lengthCHANGING
data_tab = lt_tab_fileEXCEPTIONS
file_open_error = 01
file_read_error = 02
no_batch = 03
gui_refuse_filetransfer = 04
invalid_type = 05
no_authority = 06
未知错误= 07
bad_data_format = 08
header_not_allowed = 09
splitter_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
not_supported_by_gui = 17
error_no_gui =18。

"将PDF转换为XSTRING

CALL FUNCTION'SCMS_BINARY_TO_XSTRING'

导出
input_length = lv_length

IMPORTINGbuffer = ls_xstring

TABLES
binary_tab = lt_tab_file

EXCEPTIONS
失败= 1OTHERS =2。

" HTTPS
lv_rfc_dest ='RFC_DEST'。" api.xxxxxxxx.com/v3
cl_http_client => create_by_destination(

导出
目的地= lv_rfc_dest

IMPORTINGclient = lo_http_client

EXCEPTIONS
arguments_not_found = 1
destination_not_found = 2
destination_no_authority = 3
plugin_not_active = 4
internal_error = 5OTHERS = 6)。

"登录禁用
lo_http_client-> propertytype_logon_popup = lo_http_client-> co_disabled。

"其他客户端

创建对象lo_rest_client

导出
io_http_client = lo_http_client。

lo_request = lo_rest_client-> if_rest_client〜create_request_entity()。
lo_request-> set_content_type(iv_media_type = if_rest_media_type => gc_appl_json)。


" JSON字符串
lv_body ='{" recipients [0] [name]":" Test"," recipients [0] [email]":"" test@gmail.com "}'。

lo_request-> set_string_data(lv_body)。

ld_urlurl ='/yyyyyyyyy.json'。
ls_fields-name = if_http_header_fields_sap => request_uri。
ls_fields-value = ld_urlurl.APPEND ls_fields到lt_fields。

ls_fields-name ='Author
ls_fields-value ='bearer xxxxxxxxxxxxxxxxxxx'。将ls_fields附加到lt_fields。
调用方法lo_rest_client-> if_rest_client〜set_request_headers

导出
it_header_fields = lt_fields。


* POST
lo_rest_client-> if_rest_resource〜post(lo_request)。


*响应
lo_response = lo_rest_client-> if_rest_client〜get_response_entity()。
lv_http_status = lv_status = lo_response-> get_header_field('〜status_code')。
lv_reason =(head_response- '〜status_reason')。
lv_content_length = lo_response-> get_header_field('content-length')。
lv_location = lo_response-> get_header_field('location')。
lv_content_type = lo_response-> get_header_field('content -type')。
lv_response = lo_response-> get_string_data()。

lo_rest_client-> if_rest_client〜close()。

写入:/lv_http_status,lv_reason,lv_response。

感谢和问候,

路卡

3条回答
太Q了
2020-09-02 03:22

这也解决了我的问题。 我试图使用以下方法发布图像以保留API,但无法正常工作。 API无法识别图像文件

 ****************可以正常工作***********************************  *************
 DATA(lo_mulipart)= lo_http_client-> request-> add_multipart()。

   lo_http_client-> request-> set_header_field(name ='Accept'
                                  值='application/json')。
   lo_mulipart-> set_header_field(name ='Content-Disposition'
                                 值= lv_file_form_data)。

   lo_mulipart-> set_header_field(name ='Content-Type'
                                 值='image/png')。
   lo_mulipart-> set_header_field(name ='Content-Transfer-Encoding'
                                 value ='binary')。
   lo_mulipart-> set_data(数据= lv_file_content
                          偏移量= 0
                          长度= lv_input_len)。

 

一周热门 更多>