当前位置:网站首页>ABAP call restful API
ABAP call restful API
2022-07-01 15:57:00 【InfoQ】
*"----------------------------------------------------------------------
*"*" 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'. "'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.
" 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'.
" 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.
" 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( )."' Interface connection failed '.
ev_type = 'E'.
ENDIF.
ELSE.
ev_msg = http_client->response->get_cdata( )."' Interface connection failed '.
ev_type = 'E'.
ENDIF.
ENDIF.
" 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.边栏推荐
- AVL 平衡二叉搜索树
- Pocket network supports moonbeam and Moonriver RPC layers
- Nuxt. JS data prefetching
- 并发编程系列之什么是ForkJoin框架?
- [pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)
- SAP s/4hana: one code line, many choices
- Connect the ABAP on premises system to the central inspection system for custom code migration
- laravel的模型删除后动作
- 【LeetCode】43. 字符串相乘
- 远程办公经验?来一场自问自答的介绍吧~ | 社区征文
猜你喜欢
![[每日一氵]Latex 的通讯作者怎么搞](/img/0f/d19b27dc42124c89993dee1bada838.png)
[每日一氵]Latex 的通讯作者怎么搞

Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知

Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题

Win11如何设置用户权限?Win11设置用户权限的方法

Pico,是要拯救还是带偏消费级VR?

HR面试:最常见的面试问题和技巧性答复

最新NLP赛事实践总结!

Automatic, intelligent and visual! Deeply convinced of the eight designs behind sslo scheme

马来西亚《星报》:在WTO MC12 孙宇晨仍在坚持数字经济梦想
![[IDM] IDM downloader installation](/img/2b/baf8852b422c1c4a18e9c60de864e5.png)
[IDM] IDM downloader installation
随机推荐
Win11如何设置用户权限?Win11设置用户权限的方法
The latest NLP game practice summary!
ABAP-调用Restful API
SAP s/4hana: one code line, many choices
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
从 MLPerf 谈起:如何引领 AI 加速器的下一波浪潮
Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
[one day learning awk] conditions and cycles
[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb
AVL balanced binary search tree
Redis high availability principle
ThinkPHP advanced
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(三)
她就是那个「别人家的HR」|ONES 人物
智慧党建: 穿越时空的信仰 | 7·1 献礼
Connect the ABAP on premises system to the central inspection system for custom code migration
Équipe tensflow: Nous ne sommes pas abandonnés
laravel的模型删除后动作
Automatic, intelligent and visual! Deeply convinced of the eight designs behind sslo scheme
[target tracking] |stark