Skip to Content
Author's profile photo Former Member

Consuming OData service in ABAP via CL_HTTP_CLIENT->CREATE_BY_URL

This blog explain how can we use CL_HTTP_CLIENT to call OData service in a target system. This can only be used for a simple OData serivce with minimal parameters.

CL_HTTP_CLIENT can be used  to perform the HTTP communication in ABAP.

There are two options to create an object of type CL_HTTP_CLIENT:

  1. Create by destination: A HTTP destination is pre-created in the SM59 transaction, where the information about the authentication and the service URI for the REST service access is stored.

    The factory method CL_HTTP_CLIENT=>CREATE_BY_DESTINATION is used in this case to instantiate the ABAP HTTP object.

  2. Create by URL: All required connection datails are specified on object construction, i.e. without any link to a destinat

    DATA: lo_http_client TYPE REF TO if_http_client,
                 lv_service TYPE string.
    *-------------------------------------------------------------------------------*
    ***This service returns the count of the entityset
    
    lv_service = 'http://server.company.com:port/sap/opu/odata/SAP/*SRV/*Set/$count
    
    *** Use CL_HTTP_CLIENT to consume the OData service using the method "create_by_url"
    cl_http_client=>create_by_url(
         EXPORTING
           url                = lv_service
         IMPORTING
           client             = lo_http_client
         EXCEPTIONS
           argument_not_found = 1
           plugin_not_active  = 2
           internal_error     = 3
           OTHERS             = 4 ).
     
    *** Call the following method to autheticate the user/password and client for the remote connection.
    CALL METHOD LO_HTTP_CLIENT->AUTHENTICATE(
       EXPORTING
         CLIENT = '200'
         USERNAME = '*'
         PASSWORD = '*'
         LANGUAGE = 'E'
         )
    **** Send the request
       lo_http_client->send(
         EXCEPTIONS
           http_communication_failure = 1
           http_invalid_state         = 2 ).
    
    *** Receive the respose
      lo_http_client->receive(
         EXCEPTIONS
           http_communication_failure = 1
           http_invalid_state         = 2
           http_processing_failed     = 3 ).
    
    *** Read the result 
      CLEAR lv_result .
       lv_result = lo_http_client->response->get_cdata( ).
     write: lv_result.
    
    
    
    
    
    

    ion.

    The factory method CL_HTTP_CLIENT=>CREATE_BY_URL is used to in this case to instantiate the ABAP HTTP object.

In the above example we are returning a number.

Disadvantage of this approach is that it can be used with the parameters that can be passed in the URL as input.

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Frederik Hudak
      Frederik Hudak

      I came upon this blog post and tried to use the example, but it contains a few syntax errors.

      This is what I ended up with for my use case - loading contents of a txt file from a url.

      I removed the authentication part as I didn't need it, modernized the syntax and also improved the exception handling by adding a "todo exception handling" comment.

      Here it is for future googlers:

      CLASS zcl_http_input_loader DEFINITION
        PUBLIC FINAL.
      
        PUBLIC SECTION.
          CLASS-METHODS get
            IMPORTING url           TYPE string
            RETURNING VALUE(result) TYPE string.
      
        PROTECTED SECTION.
        PRIVATE SECTION.
      ENDCLASS.
      
      
      
      CLASS zcl_http_input_loader IMPLEMENTATION.
      
        METHOD get.
      
          " todo exception handling
      
          cl_http_client=>create_by_url(
               EXPORTING
                 url                = url
               IMPORTING
                 client             = DATA(http_client)
               EXCEPTIONS
                 argument_not_found = 1
                 plugin_not_active  = 2
                 internal_error     = 3
                 OTHERS             = 4 ).
      
          http_client->send(
            EXCEPTIONS
              http_communication_failure = 1
              http_invalid_state         = 2 ).
      
          http_client->receive(
             EXCEPTIONS
               http_communication_failure = 1
               http_invalid_state         = 2
               http_processing_failed     = 3 ).
      
          result = http_client->response->get_cdata( ).
      
        ENDMETHOD.
      
      ENDCLASS.

       

      Author's profile photo Bradly Ali
      Bradly Ali

      Thank you i'm from google 🙂