如何在SAP ECC中调用外部系统提供的REST接口地址?
Postman中使用Body中参数情况,使用链接的情况
x-www-form-urlencoded
POST成功调用样例如下:
SAP中实现如下:
1. 事务码STRUST,导入对方系统证书
2. 事务码SM59配置destination
外部系统提供的接口地址:
https://dev-api.jushuitan.com/open/jushuitan/otherinout/upload
测试连接
返回200表示成功
3. 代码实现:
*&---------------------------------------------------------------------*
*& Report ZTEST_JST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*REPORT ztest_jst."发送
DATA: lo_http_client TYPE REF TO if_http_client.
DATA: lv_respon TYPE string.
DATA: lv_ssl_id TYPE ssfapplssl VALUE 'ANONYM'.
DATA: lv_string TYPE string.
DATA: lv_timestamp TYPE string.
DATA: lv_sign TYPE string.
DATA: lv_biz TYPE string.
DATA: lv_jason TYPE string.
DATA: lv_hashstring TYPE string.
DATA: message TYPE string.
DATA: msgtyp TYPE char1."请求时间,时间戳格式(Unix 纪元到当前时间的秒数),API服务端允许客户端请求最大时间误差为10分钟。
DATA: lv_sydatum TYPE sy-datum,lv_syuzeit TYPE sy-uzeit."创建客户端请求
CALL METHOD cl_http_client=>create_by_destinationEXPORTINGdestination = 'JST'IMPORTINGclient = lo_http_clientEXCEPTIONSargument_not_found = 1destination_not_found = 2destination_no_authority = 3plugin_not_active = 4internal_error = 5OTHERS = 6.
IF sy-subrc <> 0.MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITHsy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.EXIT.
ENDIF."设定传输请求内容格式以及编码格式
lo_http_client->request->set_content_type( content_type = 'application/x-www-form-urlencoded;charset=UTF-8' )."设定调用服务
lo_http_client->request->set_method( if_http_request=>co_request_method_post ).lo_http_client->propertytype_accept_cookie = lo_http_client->co_enabled.lv_sydatum = sy-datum.
lv_syuzeit = sy-uzeit.
cl_pco_utility=>convert_abap_timestamp_to_java(
EXPORTINGiv_date = lv_sydatum "sy-datumiv_time = lv_syuzeit "sy-uzeit
IMPORTINGev_timestamp = lv_timestamp ).lv_timestamp = lv_timestamp(10) - 8 * 60 * 60.
CONDENSE lv_timestamp.lv_biz = '{"external_id": "2023111311287","type": "in","is_confirm": true,"items": [{"batch_id": "","qty": 120,"sku_id": "YC.01.0001","remark": "测试","expiration_date": "","sale_price": 0,"produced_date": ""}]}'.
CONDENSE lv_biz.lv_string = '99c4cef262f34ca882975a7064de0b87' && 'access_token' && 'b7e3b1e24e174593af8ca5c397e53dad'&& 'app_key' && 'b0b7d1db226d4216a3d58df9ffa2dde5' && 'biz' && lv_biz&& 'charset' && 'utf-8' && 'timestamp' && lv_timestamp && 'version' && '2'.
CONDENSE lv_string.CALL FUNCTION 'CALCULATE_HASH_FOR_CHAR'EXPORTINGalg = 'MD5'data = lv_stringIMPORTINGhashstring = lv_hashstringEXCEPTIONSunknown_alg = 1param_error = 2internal_error = 3OTHERS = 4.
lv_sign = lv_hashstring.
TRANSLATE lv_sign TO LOWER CASE.lv_jason = 'app_key=' && 'b0b7d1db226d4216a3d58df9ffa2dde5' && '&access_token=' &&'b7e3b1e24e174593af8ca5c397e53dad' && '×tamp=' && lv_timestamp&& '&charset=' && 'utf-8' && '&version=' && '2' && '&sign=' && lv_sign&& '&biz=' && lv_biz.*在POSTMAN中,调用时自动将参数进行urlencode转换,调用如果需要,可以如上使用方法转换
*lv_time_64 = cl_http_utility=>escape_url( timestamp ). "escape_url"设置待传输内容
lo_http_client->request->set_data( cl_abap_codepage=>convert_to( source = lv_jason ) )."发送请求
lo_http_client->send( EXCEPTIONS http_communication_failure = 1http_invalid_state = 2 ).IF sy-subrc <> 0."操作失败,获取失败原因lo_http_client->get_last_error( IMPORTING message = message ).msgtyp = 'E'.
ENDIF."读取远程服务返回的处理过结果。
lo_http_client->receive( EXCEPTIONS http_communication_failure = 1http_invalid_state = 2http_processing_failed = 3 ).
IF sy-subrc <> 0 ."lv_subrc = sy-subrc.lo_http_client->get_last_error( IMPORTING message = message ).msgtyp = 'E'.
ELSE."读取返回返回内容CLEAR lv_respon.lv_respon = lo_http_client->response->get_cdata( ).* DATA: lv_xmldata TYPE xstring .
* DATA: lt_result_xml TYPE STANDARD TABLE OF smum_xmltb,
* ls_result_xml TYPE smum_xmltb.
* DATA: lt_return TYPE STANDARD TABLE OF bapiret2 .* CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
* EXPORTING
* text = lv_respon
* IMPORTING
* buffer = lv_xmldata.
*
* CALL FUNCTION 'SMUM_XML_PARSE'
* EXPORTING
* xml_input = lv_xmldata
* TABLES
* xml_table = lt_result_xml
* return = lt_return."解析JSON串TYPES: BEGIN OF lty_recdata,msg TYPE string,code TYPE i,END OF lty_recdata.DATA: ls_recdata TYPE lty_recdata.CLEAR:ls_recdata./ui2/cl_json=>deserialize( EXPORTING json = lv_responpretty_name = 'X'CHANGING data = ls_recdata ).ENDIF.
"关闭接口
CALL METHOD lo_http_client->close.