当前位置:网站首页>ABAP-调用Restful API
ABAP-调用Restful API
2022-07-01 15:32:00 【InfoQ】
*"----------------------------------------------------------------------
*"*"本地接口:
*" 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.
边栏推荐
猜你喜欢
张驰课堂:六西格玛数据的几种类型与区别
Photoshop plug-in HDR (II) - script development PS plug-in
入侵检测模型(An Intrusion-Detection Model)
【显存优化】深度学习显存优化方法
[one day learning awk] conditions and cycles
How to realize clock signal frequency division?
Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan
It's settled! 2022 Hainan secondary cost engineer examination time is determined! The registration channel has been opened!
Qt+pcl Chapter 9 point cloud reconstruction Series 2
[STM32 learning] w25qxx automatic judgment capacity detection based on STM32 USB storage device
随机推荐
Redis high availability principle
雷神科技冲刺北交所,拟募集资金5.4亿元
The last picture is seamlessly connected with the first picture in the swiper rotation picture
Basic use process of cmake
[antenna] [3] some shortcut keys of CST
Flink 系例 之 TableAPI & SQL 与 MYSQL 插入数据
MySQL 服务正在启动 MySQL 服务无法启动解决途径
S32K1xx 微控制器的硬件設計指南
【一天学awk】条件与循环
k8s部署redis哨兵的实现
Microservice tracking SQL (support Gorm query tracking under isto control)
[one day learning awk] conditions and cycles
微信小程序01-底部导航栏设置
《QT+PCL第六章》点云配准icp系列3
Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
TS报错 Don‘t use `object` as a type. The `object` type is currently hard to use
[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb
Qt+pcl Chapter 6 point cloud registration ICP Series 5
Introduction to MySQL audit plug-in
Returning to the top of the list, the ID is still weak