Technical Articles
Consume API directly into ABAP
Background
As API is getting famous with so many APIs available how can we consume it into ABAP directly without any middle wares.
One of the easiest way to call an external API directly into ABAP system using API Key.
Step 1 – What do you need –
Postman – To verify if the data is coming as expected
API Key – To fetch data form source system into ABAP
SSL Certificates – You can download the certificate and it needs to install in SAP. In SAP, certificates can be installed using t-code Strust.
Postman – We will need the parameters to get data in ABAP
Step 2 – Certificate needs to be installed in “SSL Server Standard”
Step 2 – ABAP Program
The below program has been developed on 750 but the classes exist from 730 itself and can be used
*&---------------------------------------------------------------------*
*& Report ZTEST_API
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_api.
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-s01.
PARAMETERS: p_inv TYPE belnr_d DEFAULT '12345'.
SELECTION-SCREEN: END OF BLOCK b1.
START-OF-SELECTION.
*HTTP Client Abstraction
DATA lo_client TYPE REF TO if_http_client.
*Data variables for storing response in xstring and string
DATA : lv_xstring TYPE xstring,
lv_string TYPE string,
lv_node_name TYPE string.
CLEAR : lv_xstring, lv_string, lv_node_name.
*Pass the URL to get Data
lv_string = |https://XXXXXX.com/api/XXXXX/{ p_inv }|.
*Creation of New IF_HTTP_Client Object
cl_http_client=>create_by_url(
EXPORTING
url = lv_string
IMPORTING
client = lo_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
).
IF sy-subrc IS NOT INITIAL.
* Handle errors
ENDIF.
lo_client->propertytype_logon_popup = lo_client->co_disabled.
lo_client->request->set_method( 'GET' ).
CALL METHOD lo_client->request->set_header_field
EXPORTING
name = 'XXXXXXXX'
value = ' XXXXXXXX '.
CALL METHOD lo_client->request->set_header_field
EXPORTING
name = 'Accept'
value = 'application/xml'.
*Structure of HTTP Connection and Dispatch of Data
lo_client->send( ).
IF sy-subrc IS NOT INITIAL.
* Handle errors
ENDIF.
*Receipt of HTTP Response
lo_client->receive( ).
IF sy-subrc IS NOT INITIAL.
* Handle errors
ENDIF.
END-OF-SELECTION.
*Return the HTTP body of this entity as binary data
lv_xstring = lo_client->response->get_data( ).
**Displays XML File
CALL METHOD cl_abap_browser=>show_xml
EXPORTING
xml_xstring = lv_xstring
size = cl_abap_browser=>large.
Step 4 – Output
Once the program is executed, you will be able to see the data directly in form of XML. The XML data can also be converted into internal table and be used/displayed as per requirement
Conclusion
It is simple to call API from ABAP. We have standard classes available to read data and to transform data into internal table.
Hi Anand,
Thanks for the coding.
I am trying to call SAP API in abap program. I am getting as logon failed.
lo_http_client->request->set_header_field( name = ‘Accept’ value = ‘application/xml’ ).
“API Key
lo_http_client->request->set_header_field( name = ‘APIKey’ value = ‘l0uuRz-uhiwNaUi-olg1cQ==’ ).
I am getting API key from Postman token( not sure this is correct )
When I execute Get request, I am getting as logon failed.
In your code, you mentioned to get SSL certificate and upload. I am calling SAP API, how to get this certificate?
Regards,
Sarath
Hello Ashutosh Anand.
I am getting an error in the execution of the receive method: 407 ICM_HTTP_SSL_ERROR.
Could you tell me the reason for the error?
Thank you.
Hello Douglas,
2 years after, but I just add the same issue.
For my case, SSL certificate need to be uploaded at 'SSL Client (Standard)' level, at 'SSL server standard', I had the same error.
Regards,
Guy
Hi Ashton,
Thank you for posting the information. I have registered all levels of the endpoint certification in strust but I am still getting 407 error SSLERR_NO_SSL_RESPONSE I am not sure if you have experienced same issue in the past.
Best regards,
Marta Miyazaki
Hello Ashutosh Anand,
Thanks for the blog, really helped me.
I have a question. How do I handle exceptions?
Hi Ashutosh,
We have a similar requirement and I tried to implement the process that you have given here. But I'm getting error at the receive() method call.
I'm getting the exception http_communication_failure.
Any idea what is missing here?
Thank you,
Kishore