SAP ABAP 主动调用外部系统的REST接口(x-www-form-urlencoded)

如何在SAP ECC中调用外部系统提供的REST接口地址?

Postman中使用Body中参数情况,使用链接的情况

x-www-form-urlencoded

POST成功调用样例如下:

SAP中实现如下:

  1. 事务码STRUST,导入对方系统证书
  1. 事务码SM59配置destination

外部系统提供的接口地址:

https://dev-api.jushuitan.com/open/jushuitan/otherinout/upload

测试连接

返回200表示成功

  1. 代码实现:

    &---------------------------------------------------------------------
    *& 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_destination
    EXPORTING
    destination = 'JST'
    IMPORTING
    client = lo_http_client
    EXCEPTIONS
    argument_not_found = 1
    destination_not_found = 2
    destination_no_authority = 3
    plugin_not_active = 4
    internal_error = 5
    OTHERS = 6.
    IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH
    sy-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(
    EXPORTING
    iv_date = lv_sydatum "sy-datum
    iv_time = lv_syuzeit "sy-uzeit
    IMPORTING
    ev_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'
    EXPORTING
    alg = 'MD5'
    data = lv_string
    IMPORTING
    hashstring = lv_hashstring
    EXCEPTIONS
    unknown_alg = 1
    param_error = 2
    internal_error = 3
    OTHERS = 4.
    lv_sign = lv_hashstring.
    TRANSLATE lv_sign TO LOWER CASE.

    lv_jason = 'app_key=' && 'b0b7d1db226d4216a3d58df9ffa2dde5' && '&access_token=' &&
    'b7e3b1e24e174593af8ca5c397e53dad' && '&timestamp=' && 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 = 1
    http_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 = 1
    http_invalid_state = 2
    http_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_respon
      pretty_name = 'X'
      CHANGING data = ls_recdata ).

    ENDIF.
    "关闭接口
    CALL METHOD lo_http_client->close.

相关推荐
山茶花开时。8 天前
[SAP ABAP] 序列化与反序列化
开发语言·sap·abap
左甜甜10 天前
SAP MMPV报错MMPV_DATE_CHECK
sap
SAP女战士12 天前
【ABAP SAP】开发-BUG修补记录_采购申请打印时品名规格品牌为空
运维·数据库·bug·sap·abap开发
Cindy的SAP加油站14 天前
SAP Ariba_Order Fulfillment Status
sap·sap ariba
SAP学习成长之路15 天前
ADBC 查询语法介绍:EXECUTE_QUERY
java·服务器·开发语言·数据库·sap·abap·零售
需己以听的以听17 天前
4.模块化技术之子程序
sap·gui·abap·模块化·改行学it·子程序
syounger17 天前
近四分之一SAP ECC用户对未来规划尚不明朗
sap
SAP学习成长之路22 天前
SAP HANA 上进行 ABAP 开发:简介
开发语言·数据结构·数据库·sap·abap·代码规范
阿达_优阅达25 天前
利用 Xtract Universal,将 SAP 数据高效迁移至 Amazon S3 或 Redshift
数据库·sap·企业数字化转型·theobald sap·redshift
左甜甜1 个月前
ABAP OOALV模板
sap·abap·alv