当前位置:网站首页>ABAP call restful API

ABAP call restful API

2022-07-01 15:57:00 InfoQ

SAP When integrating with other systems , Sometimes you need to call restful API Interface , Here's a demo For reference
*"----------------------------------------------------------------------
*"*" Local interface :
*" 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 = ' Please enter the interface name code '.
 ev_type = 'E'.
 RETURN.
 ENDIF.
 IF iv_body IS INITIAL .
 ev_msg = ' Please enter Request Body Content '.
 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 = ' Interface address is not configured , Please pass  zt_url  To configure OA Address of the interface '.
 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.
 " Create client request
 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'. &quot;'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.
 &quot; Send a request
 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'.
 &quot; Read the processed result returned by the remote service .
 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.
 &quot; analysis JSON strand
 /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 = ' Transmission successful '.
 ev_type = 'S'.
 ELSE.
 ev_msg = ' Return status is empty ' && http_client->response->get_cdata( ).&quot;' Interface connection failed '.
 ev_type = 'E'.
 ENDIF.
 ELSE.
 ev_msg = http_client->response->get_cdata( ).&quot;' Interface connection failed '.
 ev_type = 'E'.
 ENDIF.
 ENDIF.
 &quot; Log saving
 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.
Reproduced in :
https://blog.csdn.net/weixin_41886784/article/details/123005707
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011532468583.html

随机推荐