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


    如何在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. 代码实现:

    1. *&---------------------------------------------------------------------*
    2. *& Report ZTEST_JST
    3. *&
    4. *&---------------------------------------------------------------------*
    5. *&
    6. *&
    7. *&---------------------------------------------------------------------*
    8. REPORT ztest_jst.
    9. "发送
    10. DATA: lo_http_client TYPE REF TO if_http_client.
    11. DATA: lv_respon TYPE string.
    12. DATA: lv_ssl_id TYPE ssfapplssl VALUE 'ANONYM'.
    13. DATA: lv_string TYPE string.
    14. DATA: lv_timestamp TYPE string.
    15. DATA: lv_sign TYPE string.
    16. DATA: lv_biz TYPE string.
    17. DATA: lv_jason TYPE string.
    18. DATA: lv_hashstring TYPE string.
    19. DATA: message TYPE string.
    20. DATA: msgtyp TYPE char1.
    21. "请求时间,时间戳格式(Unix 纪元到当前时间的秒数),API服务端允许客户端请求最大时间误差为10分钟。
    22. DATA: lv_sydatum TYPE sy-datum,
    23. lv_syuzeit TYPE sy-uzeit.
    24. "创建客户端请求
    25. CALL METHOD cl_http_client=>create_by_destination
    26. EXPORTING
    27. destination = 'JST'
    28. IMPORTING
    29. client = lo_http_client
    30. EXCEPTIONS
    31. argument_not_found = 1
    32. destination_not_found = 2
    33. destination_no_authority = 3
    34. plugin_not_active = 4
    35. internal_error = 5
    36. OTHERS = 6.
    37. IF sy-subrc <> 0.
    38. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH
    39. sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    40. EXIT.
    41. ENDIF.
    42. "设定传输请求内容格式以及编码格式
    43. lo_http_client->request->set_content_type( content_type = 'application/x-www-form-urlencoded;charset=UTF-8' ).
    44. "设定调用服务
    45. lo_http_client->request->set_method( if_http_request=>co_request_method_post ).
    46. lo_http_client->propertytype_accept_cookie = lo_http_client->co_enabled.
    47. lv_sydatum = sy-datum.
    48. lv_syuzeit = sy-uzeit.
    49. cl_pco_utility=>convert_abap_timestamp_to_java(
    50. EXPORTING
    51. iv_date = lv_sydatum "sy-datum
    52. iv_time = lv_syuzeit "sy-uzeit
    53. IMPORTING
    54. ev_timestamp = lv_timestamp ).
    55. lv_timestamp = lv_timestamp(10) - 8 * 60 * 60.
    56. CONDENSE lv_timestamp.
    57. 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": ""}]}'.
    58. CONDENSE lv_biz.
    59. lv_string = '99c4cef262f34ca882975a7064de0b87' && 'access_token' && 'b7e3b1e24e174593af8ca5c397e53dad'
    60. && 'app_key' && 'b0b7d1db226d4216a3d58df9ffa2dde5' && 'biz' && lv_biz
    61. && 'charset' && 'utf-8' && 'timestamp' && lv_timestamp && 'version' && '2'.
    62. CONDENSE lv_string.
    63. CALL FUNCTION 'CALCULATE_HASH_FOR_CHAR'
    64. EXPORTING
    65. alg = 'MD5'
    66. data = lv_string
    67. IMPORTING
    68. hashstring = lv_hashstring
    69. EXCEPTIONS
    70. unknown_alg = 1
    71. param_error = 2
    72. internal_error = 3
    73. OTHERS = 4.
    74. lv_sign = lv_hashstring.
    75. TRANSLATE lv_sign TO LOWER CASE.
    76. lv_jason = 'app_key=' && 'b0b7d1db226d4216a3d58df9ffa2dde5' && '&access_token=' &&
    77. 'b7e3b1e24e174593af8ca5c397e53dad' && '×tamp=' && lv_timestamp
    78. && '&charset=' && 'utf-8' && '&version=' && '2' && '&sign=' && lv_sign
    79. && '&biz=' && lv_biz.
    80. *在POSTMAN中,调用时自动将参数进行urlencode转换,调用如果需要,可以如上使用方法转换
    81. *lv_time_64 = cl_http_utility=>escape_url( timestamp ). "escape_url
    82. "设置待传输内容
    83. lo_http_client->request->set_data( cl_abap_codepage=>convert_to( source = lv_jason ) ).
    84. "发送请求
    85. lo_http_client->send( EXCEPTIONS http_communication_failure = 1
    86. http_invalid_state = 2 ).
    87. IF sy-subrc <> 0.
    88. "操作失败,获取失败原因
    89. lo_http_client->get_last_error( IMPORTING message = message ).
    90. msgtyp = 'E'.
    91. ENDIF.
    92. "读取远程服务返回的处理过结果。
    93. lo_http_client->receive( EXCEPTIONS http_communication_failure = 1
    94. http_invalid_state = 2
    95. http_processing_failed = 3 ).
    96. IF sy-subrc <> 0 .
    97. "lv_subrc = sy-subrc.
    98. lo_http_client->get_last_error( IMPORTING message = message ).
    99. msgtyp = 'E'.
    100. ELSE.
    101. "读取返回返回内容
    102. CLEAR lv_respon.
    103. lv_respon = lo_http_client->response->get_cdata( ).
    104. * DATA: lv_xmldata TYPE xstring .
    105. * DATA: lt_result_xml TYPE STANDARD TABLE OF smum_xmltb,
    106. * ls_result_xml TYPE smum_xmltb.
    107. * DATA: lt_return TYPE STANDARD TABLE OF bapiret2 .
    108. * CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    109. * EXPORTING
    110. * text = lv_respon
    111. * IMPORTING
    112. * buffer = lv_xmldata.
    113. *
    114. * CALL FUNCTION 'SMUM_XML_PARSE'
    115. * EXPORTING
    116. * xml_input = lv_xmldata
    117. * TABLES
    118. * xml_table = lt_result_xml
    119. * return = lt_return.
    120. "解析JSON串
    121. TYPES: BEGIN OF lty_recdata,
    122. msg TYPE string,
    123. code TYPE i,
    124. END OF lty_recdata.
    125. DATA: ls_recdata TYPE lty_recdata.
    126. CLEAR:ls_recdata.
    127. /ui2/cl_json=>deserialize( EXPORTING json = lv_respon
    128. pretty_name = 'X'
    129. CHANGING data = ls_recdata ).
    130. ENDIF.
    131. "关闭接口
    132. CALL METHOD lo_http_client->close.

  • 相关阅读:
    百行代码实现VLC简易视频播放器【详细环境配置过程+可执行源码注释完整】
    LeetCode-46-全排列
    ThingsBoard 开源物联网平台
    【Python 实战基础】Pandas中Series的创建和数据类型转换
    【开源电路】STM32F401RCT6开发板
    机器人机械臂抓取综述
    SpringBoot在静态方法或工具类中注入Bean及配置参数
    Apollo之虚拟机部署
    《吐血整理》高级系列教程-吃透Fiddler抓包教程(33)-Fiddler如何抓取WebSocket数据包
    OCR文本识别网络SAR的学习
  • 原文地址:https://blog.csdn.net/weixin_37949164/article/details/134405992