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:
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.
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.
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.
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:
Thank you i'm from google 🙂