Uploaded by ömer alper

SAP REST API Programming with SICF: A Step-by-Step Guide

advertisement
Ko
la
Su
re
sh
SICF - REST API
Programming
in SAP
Overview
A REST API (Representational State Transfer Application Programming Interface)
is a type of API that uses the Representational State Transfer (REST)
architectural style for web service communication. It allows different software
applications to interact with each other by exchanging data using HTTP methods
(like GET, POST, PUT, and DELETE) and standardized formats like JSON or XML.
Example: Create a sales order in SAP system from the Web application using
JSON/XML data.
Define HTTP Handler
Ko
la
This document covers basic level programming for better understanding.
Su
re
sh
Execute SE24→Enter classname, description→save.
Enter interface “IF_HTTP_EXTENSION” →Enter.
Su
re
sh
Ko
la
Attributes are derived by the interface.
The method “IF_HTTP_EXTENSION~HANDLE_REQUEST” is derived by the
interface.
Implement method
Implement method “IF_HTTP_EXTENSION~HANDLE_REQUEST”. You need to
implement the following three steps to implement the method.
1.​ Get request data
2.​ Process data
3.​ Set response data
1.​
Get request data
Attribute
Level
Ko
la
Before getting request data, declare attributes in the class.
Visib Typi Associat Descripti
ility ng
ed type on
Purpose
Request
Type
Instanc
e
Attribut Publi
GV_SERVICE e
c
Type STRING
Validate the service call and
Requested align to the right process.
Service
ex: Createsaleorder
Su
re
sh
Instanc
e
Attribut Publi
GV_REQUEST e
c
Type STRING
GV_CDATA
Instanc
e
Attribut Publi
e
c
Type STRING
Payload
Data
Validate 'GET' or 'POST
Import payload data
Instanc
e
Attribut Publi
Response
GV_XSTRING e
c
Type XSTRING Data
Set response data
2.​
Process data
Ko
la
Derive request, service and payload data.
Su
re
sh
Create one more method in the class.
●​ You can use GV_REQUET to validate the process call. Ex: POST / GET
call.
●​ You can use GV_SERVICE to validate the process request and route it to
the right process. Ex; Create sales order in the SAP system.
●​ Export GV_CDATA to another class where this data is converted to
internal data and process the sales order.
●​ Import GV_XSTRING to set response.
NOTE: ZCL_SD_PROCESS_DATA implementation is explained in a separate
section below.
Ko
la
Su
re
sh
Once it is implemented successfully, call this method in the previous method
“IF_HTTP_EXTENSION~HANDLE_REQUEST”.
3.​
Set response data
●​ Set the response (GV_XSTRING) received from the process_transaction
method to communicate to the third party system.
Ko
la
ZCL_SD_PROCESS_DATA Implementation
In this class, following are the activities to be performed.
1.​ Convert JSON to ABAP
2.​ Process data
3.​ Prepare response data
Convert JSON to ABAP
Su
re
sh
1.​
Create one method as per the process requirement. In our example,
CREATE_SO.
Add following parameters for data processing.
Parameter
Type
Typing Associate Default
Description
Method d
Type
Importing
Type
STRING
Payload data
EX_XSTRING Exporting
Type
XSTRING
Response data
Su
re
sh
Ko
la
IM_CDATA
Value
Create types to convert payload data to internal data.
In this example, item structure, item table type and header structures were
created.
Ko
la
Su
re
sh
●​ Go back to method “CREATE_SO” →Implement the payload data
conversion.
●​ Declare the local data structure to convert payload.
●​ Use “CL_FDT_JSON=>JSON_TO_DATA” to convert data exporting
payload data and importing converted data into local structure.
Process data
Ko
la
2.​
Su
re
sh
Once the payload data is converted, distribute the data into relevant data
structures of BAPI and create sales orders using sales order BAPI.
Prepare response data
Ko
la
3.​
Su
re
sh
Convert the return messages into response data using
“CL_SXML_STRING_WRITER”.
SICF - Service
Execute SICF transaction code→Click execute.
Ko
la
Su
re
sh
Select any element and click on “Create Host/Service”.
Ko
la
Enter service element name→Click OK.
Su
re
sh
Enter the process description and logon data.
Ko
la
Add the Handler class that was defined in the first steps and save the service.
Su
re
sh
Go back to the previous screen→right click on service name→Activate service.
Test service
Su
re
sh
Ko
la
Right click on service→Test service.
●​ Upon clicking on the test service, the system shows a popup with the
executing URL.
●​ In order to execute this URL, you need to maintain HOST entry files in
your system.
●​ Copy the site name. Ex; s4hana 2022.ABC.com.
Su
re
sh
Expand etc folder.
Ko
la
Go to desktop→Press Windows+R→Enter drivers→OK. (In the case of the
Windows system.)
Right click on the hosts file→Select Edit with Notepad or Notepad++.
Ko
la
Su
re
sh
Enter the SAP system IP and copied URL. Separate it with tab space and then
save it.
●​ Download the POSTMAN application in your system→Open it.
●​ Copy the complete URL and paste it in the POSTMAN application and set
“POST”. ex;
http://s4hana2022.ABC.com:8000/sap/createsaleorder?sap-client=100
Su
re
sh
Ko
la
Click on Authorization→Select Auth type “Basic Auth” →Enter username and
password.
Prepare the data in JSON format and paste it in the body section by selecting the
radio button “raw”.
Ex raw data: {"customer": "0000010000","ordate": "20240517","ponumber":
"INC-SONUM001","itemdata": [{"itemno":"10","itemcode":
"SK-WRDRB","itemqty": "1","itemuom": "","plant":
"1501"},{"itemno":"20","itemcode": "SK-WRDRB","itemqty": "2","itemuom":
"","plant": "1501"}]}
Results
Ko
la
Click on the Send button to see the results.
Su
re
sh
Once the SEND action is triggered, the JSON data is processed in the SAP
system and then issues the response as below.
Download