写点什么

ABAP- 调用 Restful API

  • 2022 年 7 月 01 日
  • 本文字数:2023 字

    阅读完需:约 7 分钟

SAP 与其他系统集成时,有时需要调用 restful API 接口,下面是一个 demo 供参考

*"----------------------------------------------------------------------*"*"本地接口:*"  IMPORTING*"     REFERENCE(IV_CODE) TYPE  STRING*"     REFERENCE(IV_BODY) TYPE  STRING*"  EXPORTING*"     REFERENCE(EV_TYPE) TYPE  CHAR1*"     REFERENCE(EV_MSG) TYPE  STRING*"----------------------------------------------------------------------  TYPES: BEGIN OF ty_result,           success TYPE string,           errmsg  TYPE string,         END OF ty_result.  DATA: "lt_result TYPE TABLE OF ty_result,        ls_result TYPE ty_result.  DATA: lt_log TYPE TABLE OF zhrt_oa,        ls_log TYPE zhrt_oa.  IF iv_code IS INITIAL .    ev_msg = '请输入接口名称代码'.    ev_type = 'E'.    RETURN.  ENDIF.  IF iv_body IS INITIAL .    ev_msg = '请输入Request Body内容'.    ev_type = 'E'.    RETURN.  ENDIF.  DATA: lv_message TYPE string,        lv_service TYPE string.  DATA http_client  TYPE REF TO if_http_client.  CLEAR:lv_service,lv_message.  SELECT SINGLE url INTO lv_service FROM zt_url WHERE sysid = 'OA'.  IF lv_service IS INITIAL .    ev_msg = '接口地址未配置,请通过 zt_url 配置OA接口地址'.    ev_type = 'E'.    RETURN.  ELSE.    lv_service = lv_service && iv_code.  ENDIF.*  IF sy-sysid EQ 'DEV' OR sy-sysid EQ 'TES'.*    lv_service = 'http://192.168.19.244:8090/OAapp/' && iv_code.*  ELSE.*    lv_service = 'http://*****'.*  ENDIF.  CLEAR ls_log.  ls_log-zcode = iv_code.  SELECT SINGLE zname zflow INTO (ls_log-zname,ls_log-zflow)    FROM zt_interface    WHERE zcode = ls_log-zcode.  ls_log-zdatum = sy-datum.  ls_log-zuzeit = sy-uzeit.  ls_log-zpernr = sy-uname.  ls_log-request = iv_body.  "创建客户端请求  CALL METHOD cl_http_client=>create_by_url    EXPORTING      url                = lv_service    IMPORTING      client             = http_client    EXCEPTIONS      argument_not_found = 1      plugin_not_active  = 2      internal_error     = 3      OTHERS             = 4.  IF sy-subrc <> 0.    http_client->get_last_error( IMPORTING message = lv_message ).    ev_msg = lv_message.    ev_type = 'E'.  ENDIF.   IF ev_type NE 'E'.    CALL METHOD http_client->request->set_header_field      EXPORTING        name  = 'Content-Type'        value = 'application/json'. "'application/JSON; charset=utf-8'.    CALL METHOD http_client->request->set_method( 'POST' ).    IF iv_body IS NOT INITIAL.      CALL METHOD http_client->request->set_cdata        EXPORTING          data   = iv_body          offset = 0          length = strlen( iv_body ).    ENDIF.    "发送请求    CALL METHOD http_client->send      EXCEPTIONS        http_communication_failure = 1        http_invalid_state         = 2.    IF sy-subrc <> 0.      http_client->get_last_error( IMPORTING message = lv_message ).      ev_msg = lv_message.      ev_type = 'E'.    ENDIF.  ENDIF.   IF ev_type NE 'E'.    "读取远程服务返回的处理过结果。    CALL METHOD http_client->receive      EXCEPTIONS        http_communication_failure = 1        http_invalid_state         = 2        http_processing_failed     = 3.    IF sy-subrc = 0.      DATA(result) = http_client->response->get_cdata( ).      ls_log-response = result.      "解析JSON串      /ui2/cl_json=>deserialize( EXPORTING json = result pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = ls_result ).       IF ls_result-success = '0'.        ev_msg = ls_result-errmsg.        ev_type = 'E'.      ELSEIF ls_result-success = '1'..        ev_msg = '传输成功'.        ev_type = 'S'.      ELSE.        ev_msg = '返回状态为空' && http_client->response->get_cdata( )."'接口连接失败'.        ev_type = 'E'.      ENDIF.    ELSE.      ev_msg = http_client->response->get_cdata( )."'接口连接失败'.      ev_type = 'E'.    ENDIF.  ENDIF.  "日志保存  ls_log-ztype = ev_type.  ls_log-zmsg = ev_msg.  APPEND ls_log TO lt_log.  MODIFY zt_log FROM TABLE lt_log.  COMMIT WORK.
复制代码

转载于:https://blog.csdn.net/weixin_41886784/article/details/123005707

用户头像

还未添加个人签名 2022.06.08 加入

还未添加个人简介

评论

发布
暂无评论
ABAP-调用Restful API_abap_桥下本有油菜花_InfoQ写作社区