Skip to Content

Consuming REST based service from ABAP

Hello,

My requirement is to consume a REST service in ABAP. I have written the following program but instead of getting the JSON result I am getting the HTML of the page. Can someone please let me know where I am going wrong.

lv_url = <REST service URL>

cl_http_client=>create_by_url(
EXPORTING
url = lv_url
proxy_host = 'proxy.khof.com'
proxy_service = '8080'

IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4 ).
IF sy-subrc <> 0.
RETURN.
ENDIF.


* HTTP basic authenication
lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
l_username = <username>.
l_password = ''.
CALL METHOD lo_http_client->authenticate
EXPORTING
username = l_username
password = l_password.


* Create REST client instance
CREATE OBJECT lo_rest_client
EXPORTING
io_http_client = lo_http_client.


* Set HTTP version
lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_0 ).
IF lo_http_client IS BOUND AND lo_rest_client IS BOUND.
DATA(id) = 'securityUserName=<username>&domain=S1'.
CONCATENATE lv_url '?' id INTO lv_url.
* Set the URI if any
cl_http_utility=>set_request_uri(
EXPORTING
request = lo_http_client->request " HTTP Framework (iHTTP) HTTP Request
uri = lv_url " URI String (in the Form of /path?query-string)
).
* Set request header if any

token = <token for REST service>

CALL METHOD lo_rest_client->if_rest_client~set_request_header
EXPORTING
iv_name = 'auth-token'
iv_value = token.
* HTTP GET
lo_rest_client->if_rest_client~get( ).

* HTTP response
lo_response = lo_rest_client->if_rest_client~get_response_entity( ).

* HTTP return status
DATA(http_status) = lo_response->get_header_field( '~status_code' ).

* HTTP JSON return string
DATA(json_response) = lo_response->get_string_data( ).
* Class to convert the JSON to an ABAP sttructure
DATA lr_json_deserializer TYPE REF TO cl_trex_json_deserializer.
CREATE OBJECT lr_json_deserializer.
* lr_json_deserializer->deserialize( EXPORTING json = json_response IMPORTING abap = abap_response ).
ENDIF.


Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Apr 16 at 09:39 AM

    First question: do you get a 2xx result code?
    I got a scenario where the result is an HTML page because i got a 404 :)
    Second question: do the service you are invoking has any parameter to choose how to return the payload?

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 16 at 01:33 PM

    Thanks for your response. The HTTP status is coming as 200.

    The service has two parameters securityUserName and domain which appear as input fields when we run the service on the browser.

    Add comment
    10|10000 characters needed characters exceeded