当前位置:网站首页>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.
边栏推荐
- Connect the ABAP on premises system to the central inspection system for custom code migration
- Does 1.5.1 in Seata support mysql8?
- Please, stop painting star! This has nothing to do with patriotism!
- process. env. NODE_ ENV
- Nuxt. JS data prefetching
- How to adjust the color of the computer screen and how to change the color of the computer screen
- ATSS:自动选择样本,消除Anchor based和Anchor free物体检测方法之间的差别
- TensorFlow團隊:我們沒被拋弃
- Smart Party Building: faith through time and space | 7.1 dedication
- STM32ADC模拟/数字转换详解
猜你喜欢
Zhang Chi Consulting: lead lithium battery into six sigma consulting to reduce battery capacity attenuation
使用腾讯云搭建图床服务
智慧党建: 穿越时空的信仰 | 7·1 献礼
[one day learning awk] function and user-defined function
分享在大疆DJI(深圳总部)工作的日常和福利
Tanabata confession introduction: teach you to use your own profession to say love words, the success rate is 100%, I can only help you here ~ (programmer Series)
搜索框和按钮缩放时会有缝隙的bug
你TM到底几点下班?!!!
Go language learning notes - Gorm use - table addition, deletion, modification and query | web framework gin (VIII)
Microservice tracking SQL (support Gorm query tracking under isto control)
随机推荐
Microservice tracking SQL (support Gorm query tracking under isto control)
TensorFlow团队:我们没被抛弃
Please, stop painting star! This has nothing to do with patriotism!
Stm32f411 SPI2 output error, pb15 has no pulse debugging record [finally, pb15 and pb14 were found to be short circuited]
[PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system
[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)
How does win11 set user permissions? Win11 method of setting user permissions
关于用 ABAP 代码手动触发 SAP CRM organization Model 自动决定的研究
马来西亚《星报》:在WTO MC12 孙宇晨仍在坚持数字经济梦想
最新NLP赛事实践总结!
Share the daily work and welfare of DJI (Shenzhen headquarters) in Dajiang
Hardware development notes (9): basic process of hardware development, making a USB to RS232 module (8): create asm1117-3.3v package library and associate principle graphic devices
MySQL advanced 4
picgo快捷键 绝了这人和我的想法 一模一样
RT-Thread Env 工具介绍(学习笔记)
Is JPMorgan futures safe to open an account? What is the account opening method of JPMorgan futures company?
[IDM] IDM downloader installation
工厂高精度定位管理系统,数字化安全生产管理
Overview | slam of laser and vision fusion
Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题