Introduction to RESTful APIs:
RESTful APIs provide a simple way to interact with web services, making them a powerful tool for SAP developers. In this blog post, we'll explore how to call RESTful APIs from SAP ABAP, covering everything from setup to practical examples.
Some basic terms while calling API.
⦁ API: API stands for Application Programming Interface. Are set of rules that allow programs to communicate with each other.
⦁ REST: REST stands for Representational State Transfer. It is an architectural style for providing standards between systems on web which makes easier for systems to communicate with each other.
⦁ REST API: The API’s which follows REST methodologies for the communication. In this, communication is done in form of Request and Response. Response is nothing but a request from server to the client.
⦁ Request is made up of mainly 4 things
1) Endpoint: It is the URL you request for, it contains the target host and the prefix path.
2) Methods: Rest API generally uses four methods to communicate with server.
Methods
a) Get - This method is used to get data from the server
b) Post - This method is used to create new data entry on the server
c) Put - This method is used to update any data on the server. It will replace the entire resources with provided data.
d) Patch - This method is also used to update any data on the server like put only difference is it allow you to update specific fields or properties that you want to change.
e) Delete - This method is used to delete data from the server
3) Headers: Headers are used to provide authentication and provide information of the body content.
4) The Body: The data you want to send to the server is sent through the request body. This data is sent either in JSON format or XML format.
Prerequisites:
Ensure you have the necessary authorizations to call HTTP requests. You might need the following:
⦁ HTTP Service: Ensure that the HTTP service is enabled in your SAP system. This can be checked and configured in transaction SICF (SAP Internet Communication Framework).
⦁ Authorization: Make sure your user has the necessary permissions to make HTTP calls and manage the HTTP destination.
Setting Up the Environment:
After executing SM59 below screen will be visible.
1) ABAP Connections: Type 3 means Connection to ABAP System.
2) HTTP connection to External Server: Type G means we will connect SAP system with Non - SAP system or third party system.
3) HTTP connection to ABAP System: Type H means HTTP Connection to ABAP System
4) Internal Connections: Type I means connection to Application Server with Same Database.
5) Logical Connections: Type L means Reference Entry (Refers to Other Destination)
6) TCP/IP Connections: Type T means start External Program Using TCP/IP.
7) Connection via ABAP Driver: Type X means RFC Using Special ABAP Driver Routines.
Connecting SAP with Non - SAP System
So, we are going to connect SAP with Non - SAP system. For that we need to create RFC connection of type G i.e. HTTP Connections to External Server. Below is the procedure for creating connection.
1) Firstly, we need to click on create button as below.
2) After clicking on create button below screen will be visible. We need to enter target host i.e. URL of third party.
⦁ For HTTP connection.
- Give Service number (Port number) : 80
- Select SSL as inactive in ‘Logon & Security’.
⦁ For HTTPS Connection.
- Give Service number (Port number): 443.
- Configure proxy settings with help of BASIS team.
- Configure SSL certificate in t-code ‘STRUST’ with help of BASIS team.
- Select SSL as active and select ‘ANONYM SSL’ as shown in picture below.
(Difference between HTTP & HTTPS is HTTPS provides a secure, encrypted connection that helps protect your data and privacy, whereas HTTP does not.).
3) Then click on ‘Connection Test’ button on upper left corner in order to test the connection we are trying to establish. If following screen appears we can assume that connection is established successfully.
4) And after that we need to upload that SSL certificates if necessary using Transaction Code ‘STRUST’ under ‘SSL client SSL Clent (Standard) ‘as maintain below.
Making HTTP Requests from ABAP:
There is special class in SAP called ‘CL_REST_HTTP_CLIENT’ which can be used to communicate with REST API. In this class there are 2 methods for calling API.
1) 1st Method is ‘CREATE_BY_DESTINATION’
⦁ Step 1: First we have to create HTTP object for the URL destination we want to access.
Code:
DATA: LO_HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT.
CL_HTTP_CLIENT=>CREATE_BY_DESTINATION (
EXPORTING
DESTINATION = 'E-Invoicing' "Name of the RFC destination
IMPORTING
CLIENT = LO_HTTP_CLIENT " HTTP Client object
EXCEPTIONS
ARGUMENT_NOT_FOUND = 1
DESTINATION_NOT_FOUND = 2
DESTINATION_NO_AUTHORITY = 3
PLUGIN_NOT_ACTIVE = 4
INTERNAL_ERROR = 5
OTHERS = 6
).
⦁ Step 2: Set the method and version you want to use for the communication, here we have used POST method. And HTTP version as 1.0.
Code:
lo_http_client->request->set_method(if_http_request=>co_request_method_post ).
lo_http_client->request->set_version(if_http_request=>co_protocol_version_1_1 ).
⦁ Step3: Set request parameters, if any.
Code:
lv_url = /test.com/ewb/enc/v1.03/authentication'.
CALL METHOD cl_http_utility=>set_request_uri
EXPORTING
request = lo_http_client->request
uri = lv_url.
⦁ Step4: Set the request header if any.
Code:
CALL METHOD lo_http_client->request->set_header_field
EXPORTING
IV_NAME = 'Authorization'
IV_VALUE = ‘l7xxba7aa16e968646b993426bsd6734h:201806034242’.
⦁ Step5: Set the type of data you want to send to request body. Below are some examples.
⦁ If you are sending JSON pass ‘application/json’.
⦁ If you are sending XML data pass ‘application/xml’ or ‘text/xml’.
⦁ IF you are sending multipart data pass ‘multipart/form-data’.
Code: (Here I am sending JSON file)
LO_HTTP_CLIENT->REQUEST->SET_CONTENT_TYPE( 'application/json' ).
⦁ Step6: Pass actual data to the request. In following example lv_string contains JSON string which is we are going to pass to request.
Code:
DATA : lv_string type string.
LO_HTTP_CLIENT->REQUEST->SET_CDATA( LV_STRING ).
⦁ Step7: Send request to the API and receive the response.
Code:
call method lo_http_client->send “sending request to API
exceptions
http_communication_failure = 1
http_invalid_state = 2.
call method lo_http_client->receive “receiving response from API
Exceptions
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
⦁ Step8: Read HTTP return code and the response. Example if 200 is a success return code, 500 for internal server error, 400 for bad request. In the below example we get the response from the API in the string ‘response’.
Code:
DATA: http_status type string,
Reason type string,
Response type string.
call method lo_http_client->response->get_status
importing
code = http_status
reason = reason.
response = lo_http_client->response->get_cdata( ).
2) 2nd method is 'CREATE_BY_URL'
⦁ Step 1: First we have to create HTTP object for the URL destination we want to access.
Code:
DATA: lo_http_client TYPE REF TO cl_http_client,
lv_url TYPE string,
lv_response TYPE string,
lv_status TYPE i.
lv_url = 'https://api.example.com/data'.
cl_http_client=>create_by_url(
EXPORTING
url = lv_url
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4 ).
⦁ Step 2: Set the method and version you want to use for the communication, here we have used POST method. And HTTP version as 1.0.
Code:
lo_http_client->request->set_method( 'POST' ).
lo_http_client->request->SET_VERSION(if_http_request=>co_protocol_version_1_1 ).
⦁ Step 3: Set the request header if any necessary.
Code:
lt_http = VALUE #( ( name = 'accesstoken' value = ‘12344566743’ )
( name = 'apiaccesskey' value = ’ER452DD234’) ( name = 'Content-Type' value = 'application/json' ) ).
lo_http_client->request->set_header_fields( fields = lt_http ).
lo_http_client->request->set_content_type( 'application/json' ).
⦁ Step 4: Pass actual data to the request. In following example lv_string contains JSON string which is we are going to pass to request.
Code:
lo_http_client->request->set_data( lv_string).
⦁ Step 5: Send request to the API and receive the response.
Code:
lo_http_client->send( “Sending the request
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2 ).
lo_http_client->receive( “Receiving the response
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ).
⦁ Step 6: Read HTTP return code and the response. Example if 200 is a success return code, 500 for internal server error, 400 for bad request. In the below example we get the response from the API in the string ‘response’.
Code:
Data: lv_response TYPE string,
lv_http_return_code TYPE I .
lv_response = lo_http_client->response->get_cdata( ).
lo_http_client->response->get_status(IMPORTING code = lv_http_return_code ).
Conclusion:
Connecting RFCs in SAP is a powerful way to integrate different systems and enable seamless communication across your SAP landscape. By following above steps you can set up your RFC.
Feel free to reach out if you have any questions or run into issues during your implementation.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
14 | |
9 | |
7 | |
7 | |
5 | |
5 | |
4 | |
4 | |
3 | |
3 |