Skip to Content
Author's profile photo suhas gavas

Call external APIs and retrieve the data in ABAP

Hello All,

As the ODATA protocol is getting famous nowadays, and so many APIs avail different services/data as ODATA service, how an ABAPer can male use of it?

Here is one of the way to call an external API and get the data into ABAP system.

 

Step 1. Use class CL_HTTP_CLIENT to get an object of type IF_HTTP_Client. There are different methods for getting this object. example: by the API URL or by HTTP Destination etc.

In below code snippet I have used API URL to get the object. Once the object of type IF_HTTP_Client is received, then execute the instance methods SEND() and RECEIVE() to establish Connection and Dispatch of Data and receive HTTP Response.

 

 

REPORT zsuhas.

*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.

*Put required node name into constant (case sensitive)
CONSTANTS : co_req_node TYPE string VALUE '<YOUR_REQUIRED_NODE_NAME>'.

*Creation of New IF_HTTP_Client Object
cl_http_client=>create_by_url(
EXPORTING
  url                = "URL
  proxy_host         = "Proxy
  proxy_service      = "Port
  sap_username       = "Username
  sap_client         = "Client
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.

*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.

 

Note : If you might be asked to authenticate while calling RECEIVE method. If so, insert the following piece of code after you get the create object of type IF_HTTP_client.

 

lo_client->propertytype_logon_popup = lo_client->co_disabled.
  data l_username type string.
  data l_password type string.
  l_username = MY_USERNAME.
  l_password = MY_PASS.

  lo_client->authenticate(
  exporting
      proxy_authentication = 'X'
      client               = Client    " R/3 System, client number from logon
    username             = l_username    " ABAP System, User Logon Name
    password             = l_password    " Logon ID
*      language             = language    " SAP System, Current Language
    ).

 

Step 2 . Use get_data() method of the attribute response of object received in Step 1 to get the data in Binary format. And store it in a variable of type xstring.

*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 3. User standard procedure for XML processing in OO ABAP. as below.

*Create the factory object
DATA(lo_ixml)               = cl_ixml=>create( ).

*Create a factory for input and output streams
DATA(lo_stream_factory)     = lo_ixml->create_stream_factory( ).

*Create input stream for data source byte string
DATA(lo_istream)            = lo_stream_factory->create_istream_xstring( string = lv_xstring ).

*Create an XML document in DOM format
DATA(lo_document)           = lo_ixml->create_document( ).

*Create a parser
 DATA(lo_parser)            = lo_ixml->create_parser(
             stream_factory = lo_stream_factory
             istream        = lo_istream
             document       = lo_document ).


DATA(lv_rc) = lo_parser->parse( ).

IF lv_rc <> ixml_mr_parser_ok.
*  ... "Error handling
  RETURN.
ENDIF.

 

Step 4.  Get the root node of your DOM structure and all its children. Use the iterator object to reach your desired node. Once the desired node is reached, create another iterator object to iterate the desired node. the constants “co_node_text”, “co_node_element” and “co_node_cdata_section” help determine what kind of node type is read.

At this stage the data of the desired node is available in your ABAP system to play with. I am using write statement below to display the node element name and its corresponding value. Any operation can be performed once the data is brought into your SAP system.

 

DO.
  DATA(req_node) = lo_iterator_req->get_next( ).
  IF req_node IS INITIAL.
    EXIT.
  ENDIF.

*Returns the node type
  DATA(lv_nodetype) = req_node->get_type( ).

*Name
  IF lv_nodetype = if_ixml_node=>co_node_element. "Element
    IF req_node->get_name( ) NE co_req_node.
      WRITE : req_node->get_name( ).
      WRITE : ':'.
    ENDIF.
  ENDIF.

*Value
  IF lv_nodetype = if_ixml_node=>co_node_text OR lv_nodetype =  if_ixml_node=>co_node_cdata_section. "Literal Text/CDATA Section
    WRITE : req_node->get_value( ).
    SKIP.
  ENDIF.
ENDDO.

 

Assigned tags

      13 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Javier Caceres
      Javier Caceres

      Good Blog  suhas

      Author's profile photo suhas gavas
      suhas gavas
      Blog Post Author

      Thanks!!

      Author's profile photo Deepak Appu
      Deepak Appu

      Good information. Is there any web service URL you can give an example of to test this out?

       

      Author's profile photo suhas gavas
      suhas gavas
      Blog Post Author

      Hello Deepak,

      Go to the following link and signup for 90 day free weather API. Upon signup, you will receive an API KEY with which you can utilize the real time weather API data of all the major cities of world in the form of XML or Jason.

      https://developer.worldweatheronline.com/api/

      Author's profile photo Sourav Rai
      Sourav Rai

      Hi Sir,

      Can you show us how to encrypt in sap while calling  api , encryption of RSA algorithm in sap

      Author's profile photo Former Member
      Former Member

      Hi Suhas,

      Nice article.

      Could you please have a look at my requirement and suggest?

      Thanks & Regards,

      Praveen

      Author's profile photo Former Member
      Former Member

      Hi Sugas,

      I'm new to ABAP and in your code I have some doubts,I need to clarify two things from you

      1.Do we need to create a xml file before requesting the service?or is there any way to convert the string to xml?and in which point in the program we are giving the input xml file

      2.What do you mean by ?

      <YOUR_REQUIRED_NODE_NAME>

       

      Please help me.

      Thanks and Best Regards

      Venu

       

      Author's profile photo Nooruddin Bohra
      Nooruddin Bohra

      Hi Suhas,

       

      Nice Article. Can you please provide the declaration for lo_iterator_req.

      Regards,

      Nooruddin Bohra

      Author's profile photo Dung Vo The
      Dung Vo The

      Hi Nooruddin Bohra,

      data(lo_iterator_reqlo_document->create_iterator).

      Author's profile photo Karthik Spike
      Karthik Spike

      Hi Suhas,

       

      It's really a Nice article, do you have any idea how to do other way round like, from SAP to Web..

       

      Regards,

      Karthik

      Author's profile photo Suman Vyas
      Suman Vyas

      Hi Suhas,

      I have requirement to call external webservice API, json/xm into ABAP report.

      From your example, kindly help me if i pass co_req_node from https://developer.worldweatheronline.com/api/ then do i need to pass URL https://developer.worldweatheronline.com.

       

      or there is some other URL.

      I am completely new to this, kindly help.

       

       

      Thanks .

      Regards,

      Suman

      Author's profile photo Kenneth Moore
      Kenneth Moore

      If someone has done this with an SAP Concur API, I'd love it if they could share the ABAP code!

      Author's profile photo Sandeep Rattu
      Sandeep Rattu

      Hello,

      I am unable to get data from api. Could you please help me.