Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
vamsixk
Active Participant

This blog would have not been possible, but for the awesome community that is SCN. I would like to thank every contributor ever for helping me out in my hours of need!.

I had tremendous help in navigating through my current requirement thanks to the below blog posts.

How to consume an OData service using OData Services Consumption and Integration (OSCI)

Thank you. andre.fischer

Consuming an External RESTful Web Service with ABAP in Gateway

And paul.mindset

both these blogs helped me understand the intricacies of the functionality that is Consuming an OData Service.

this Blog can be considered as an extension of the Blog by paul.mindset.

we can consume a data service by using the method CREATE_BY_URL of the class CL_HTTP_CLIENT, but when authentication is involved this method was ill suited for it.

The CREATE_BY_DESTINATION method however enables us to store the Credentials in a more standard and secured fashion.

The Requirement:-

I needed to access an OData service that was exposed by HANA . It is required that we trigger this service from the ECC system and process the result.

The user would be logging in to ECC directly and not via portal, thus it would require the use of an RFC destination Login Credentials.

The Process:-

Step 1.

we have to create the RFC connection in SM59 as below.

  1. Go to TCode SM59
  2. Click on the create new connection button.
  3. Provide the description
  4. Choose the connection type as G.
  5. enter the Host name/port number(an Odata Service generally has a URL containing ".COM" followed by ":" Port Number)
    1. The  part until the ".com" without the HTTP:// is the host name
    2. The part after the ":" is the port number
  6. Enter any proxy details if a proxy is being used(we were not in my case)
  7. go to the Logon & Security tab
    1. CHoose the Basic Authentication Radio Button.
    2. Enter the logon credentials
  8. Save and click on connection test.
  9. If all setting are proper you should be able to see a webapage that is relevant to the Host you entered, in the Response Body/Response Text tabs.

Step2.

Now that we have created the RFC connection we proceed to the creation of the HTTP client .

to create the client we use the attached  code. CL_HTTP_CLIENT.txt.

DATA:   lo_http_client TYPE REF TO if_http_client,

        lv_service TYPE string,

        lv_result TYPE string.

"xml variables

DATA:    lo_ixml TYPE REF TO if_ixml,

        lo_streamfactory TYPE REF TO if_ixml_stream_factory,

        lo_istream TYPE REF TO if_ixml_istream,

        lo_document TYPE REF TO if_ixml_document,

        lo_parser TYPE REF TO if_ixml_parser,

        lo_weather_element TYPE REF TO if_ixml_element,

        lo_weather_nodes TYPE REF TO if_ixml_node_list,

        lo_curr_node TYPE REF TO if_ixml_node,

        lv_value TYPE string,

        lv_node_length TYPE i,

        lv_node_index TYPE i,

        lv_node_name TYPE string,

        lv_node_value TYPE string.

************************************************************************

* lv_ destination will be name of the RFC destination we created in SM59

************************************************************************

CALL METHOD cl_http_client=>create_by_destination

  EXPORTING

    destination              = lv_destination

  IMPORTING

    client                   = lo_http_client

  EXCEPTIONS

    argument_not_found       = 1

    destination_not_found    = 2

    destination_no_authority = 3

    plugin_not_active        = 4

    internal_error           = 5

    OTHERS                   = 6.

IF sy-subrc <> 0.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

*************************************************************************************************

* we need to build the URI, this is the part in the OData Service that comes after the port number

* This includes the Path and Query string for the service that is being called on the host.

* lv_uri holds the path and query string

*************************************************************************************************

CALL METHOD cl_http_utility=>set_request_uri

  EXPORTING

    request = lo_http_client->request

    uri     = lv_uri.

lo_http_client->receive(

  EXCEPTIONS

    http_communication_failure = 1

    http_invalid_state         = 2

    http_processing_failed     = 3 ).

**************************************************

* Making sense of the result parsing the XML

**************************************************

lv_result = lo_http_client->response->get_cdata( ).

lo_ixml = cl_ixml=>create( ).

lo_streamfactory = lo_ixml->create_stream_factory( ).

lo_istream = lo_streamfactory->create_istream_string(

                                 lv_result ).

lo_document = lo_ixml->create_document( ).

lo_parser = lo_ixml->create_parser(

                       stream_factory = lo_streamfactory

                       istream        = lo_istream

                       document       = lo_document ).

"This actually makes the XML document navigable

lo_parser->parse( ).

DATA: lv_name TYPE string.

"Navigate XML to nodes we want to process

*lo_weather_element = lo_document->get_root_element( ).

lv_name = 'content'.

lo_weather_element = lo_document->find_from_name( lv_name ).

lo_weather_nodes = lo_weather_element->get_children( ).

"Move through the nodes and assign appropriate values to export

lv_node_length = lo_weather_nodes->get_length( ).

lv_node_index = 0.

WHILE lv_node_index < lv_node_length.

  lo_curr_node = lo_weather_nodes->get_item( lv_node_index ).

  lv_node_name = lo_curr_node->get_name( ).

  lv_node_value = lo_curr_node->get_value( ).

  ADD 1 TO lv_node_index.

ENDWHILE.



Hope this Helps!, let me know if i can clarify further.


Peace!!!!

30 Comments
Labels in this area