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.


New NetWeaver Information at SAP.com

Very Helpfull

User Rating: Be the first one !