Introduction
ZCL_REST_CLIENT
- RESTful-API consumer enhancement - 自動記錄request/response
History
Version | Date | Name | Description |
---|---|---|---|
V001 | 2020/10/14 | Huang Roy | 初版 |
Public Method
HANDLE_REQUEST
- Interface: IF_HTTP_EXTENSION method
Parameter | Level | Pass Value | Optional | Type(Ref To) | Default | Description |
---|---|---|---|---|---|---|
I_ADDRESS | Importing | AD_SMTPADR | E-Mail Address | |||
I_TYPE | Importing | X | USC_Y_RECV_TYPE | 'N' | Recipient Type(N/C/B) |
Parameter Level Val Opt Type(Ref To) Default Description
SERVER Importing IF_HTTP_SERVER HTTP Server
Protected Method
_HANDLE_REQUEST
實際修改程式碼的method
Parameter Level Val Opt Type(Ref To) Default Description
SERVER Importing IF_HTTP_SERVER HTTP Server
Sample
建立RESTful API Class
1.T-code: SE24,建立Global Class,這邊用YCL_REST做範例
Superclass設定ZCL_REST_SERVER,繼承method
{width="6.6658333333333335in" height="2.6871642607174104in"}
2.滑鼠游標定位在method: _HANDLE_REQUEST,並點擊{width="0.1978915135608049in" height="0.1874770341207349in"},Redefine
{width="6.28301946631671in" height="1.9061362642169728in"}
3.修改code,並Activate。
※1.取得**~request_method**,確定方法是GET/POST/PUT/DELETE...,這裡判斷**GET**才回傳值
※2.get_form_fields(),取得的參數名會自動轉小寫
※3.最後Response
※4.另外說明,如果需要有錯誤,就直接Raise Exception讓這個Service死就好了,不用Catch
+----------------------------------------------------------------------+ | * Process | | | | DATA**: **l_method TYPE string**,** | | | | lt_param TYPE tihttpnvp**,** | | | | l_str TYPE string**.** | | | | FIELD-SYMBOLS <lw_param> TYPE ihttpnvp**.** | | | | * Process | | | | ** Get ~request_method | | | | l_method **= **server->re | | quest->get_header_field**( **name **= **'~request_method' **).** | | | | CHECK l_method **= **'GET'**.** | | | | ** Get Parameter(all parameter name is lowercase) | | | | server-> | | request->get_form_fields**( **CHANGING fields **= **lt_param **).** | | | | READ TABLE lt_param ASSIGNING <lw_param> | | | | WITH KEY name **= **'type'**.** | | | | IF sy-subrc **= **0**.** | | | | ** Response | | | | CASE <lw_param>-value**.** | | | | WHEN 'TEXT'**.** | | | | l_str **= **'TEXT test'**.** | | | | server-> | | response->set_content_type**( **'text/html; charset=utf-8' **).** | | | | WHEN 'JSON'**.** | | | | l_str **= **'{"JSONValue":"JSON test"}'**.** | | | | server->respons | | e->set_content_type**( **'application/json; charset=utf-8' **).** | | | | ENDCASE**.** | | | | server->resp | | onse->set_status**( **code **= **'200' reason **= **'OK' **).** | | | | server->response->set_cdata**( **l_str **).** | | | | ENDIF**.** | +----------------------------------------------------------------------+
設定API路徑
1.T-code: SICF,執行
※在Windows AP上執行
{width="6.707494531933508in" height="4.572344706911636in"}
2.我們在default_host→sap下,建立一個sub element,命名為rest-test
{width="2.905886920384952in" height="0.624922353455818in"}
{width="4.8848064304461944in" height="2.103904199475066in"}
3.Description 1必填,然後切到Handler List,將前面建立的Class填入
{width="6.53043416447944in" height="4.0619925634295715in"}
4.存檔後,記得啟用此Service
{width="2.2288877952755906in" height="2.0309962817147857in"}
5.Test Service
{width="2.312211286089239in" height="1.781026902887139in"}
6.自動開啟browser,輸入帳號密碼
{width="7.480555555555555in" height="2.2291666666666665in"}
7.因為程式碼中有用type來判斷回傳結果,所以改一下URL
http://eccqdap.sapecc.inventec:8010/sap/rest-test**?type=JSON**
{width="1.9789195100612424in" height="0.34370734908136485in"}
http://eccqdap.sapecc.inventec:8010/sap/rest-test**?type=TEXT**
{width="0.9165518372703412in" height="0.22913823272090988in"}
Logs
1.路徑設定於Table: ZTCFG_TOOLS,MAIN=ZCL_REST_CLIENT,SUB1=LOG_BASEPATH
{width="6.61375656167979in" height="1.489397419072616in"}
2.以資料夾(資料夾不存在會自動建立)區分環境log
{width="0.6561679790026247in" height="0.6978291776027996in"}
3.log檔名為<ClassName>_<date>
{width="6.217972440944882in" height="0.1978915135608049in"}
※目前只能在Windows AP下存Log,所以用Unix AP會跳錯誤。
※不過目前IEC開放RESTful Server的host也只有Windows AP,所以根源上直接就解決這個問題
4.檔案內資訊
{width="7.480555555555555in" height="2.660416666666667in"}
Appendix
SMICM
T-code: SMICM,點擊{width="0.1981124234470691in" height="0.1981124234470691in"}進入services
{width="6.8533103674540685in" height="4.551514654418198in"}
檢視host name, port, & timeout limit(但只能查當下登入的AP server)
{width="7.480555555555555in" height="1.163888888888889in"}
※也可以使用FM: /SDF/GET_ICM_VIRT_HOST_DATA,有多AP server時使用較方便。
各環境HostName, Port
+------------+--------------------------------------+ | System | HostName + port | +============+======================================+ | DEV | http://eccqdap.sapecc.inventec:8010 | | | | | | https://eccqdap.sapecc.inventec:5210 | +------------+--------------------------------------+ | QAS | http://eccqdap.sapecc.inventec:8000 | +------------+--------------------------------------+ | PRD | http://eccap1.sapecc.inventec:8000 | | | | | | http://eccap1.sapecc.inventec:8001 | | | | | | http://eccap2.sapecc.inventec:8000 | | | | | | http://eccap2.sapecc.inventec:8001 | | | | | | http://eccap3.sapecc.inventec:8000 | | | | | | http://eccap3.sapecc.inventec:8001 | | | | | | http://eccap4.sapecc.inventec:8000 | | | | | | http://eccap4.sapecc.inventec:8001 | +------------+--------------------------------------+
标签:http,ZCL,sapecc,REST,height,width,CLIENT,inventec,method From: https://www.cnblogs.com/TYAING/p/17413564.html