cancel
Showing results for 
Search instead for 
Did you mean: 

How to connect to an external RESTful webservice via single authentication token?

Former Member
0 Kudos

Hi Experts,

I have a requirement where i need to consume an external RESTful webservice in SAP system with single token authentication. We will be using this webservice to validate the addresses which will be created in SAP system. URL is a normal https type url.

The basis team has already created a connection in SM59 but test connection is giving an error because there is no username and password maintained there ( this being a single token webservice ).

I am using the below code to consume the webservice in ABAP :

DATA: lo_http_client TYPE REF TO if_http_client,
      lo_rest_client TYPE REF TO cl_rest_http_client,
      lv_payload     TYPE string,
      lv_response    TYPE string,
      lv_http_reason TYPE string,
      lv_payload_x   TYPE xstring,
      lv_http_code   TYPE i,
      lo_response    TYPE REF TO if_rest_entity.


cl_http_client=>create_by_url(
  EXPORTING
    url = 'https://********'
  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.

****When using the SM59 connection created by basis team.

**cl_http_client=>create_by_destination(
** EXPORTING
** destination = 'TEST' " Logical destination (specified in function call)
** IMPORTING
** client = lo_http_client " HTTP Client Abstraction
** EXCEPTIONS
** argument_not_found = 1
** destination_not_found = 2
** destination_no_authority = 3
** plugin_not_active = 4
** internal_error = 5
** OTHERS = 6
** ).

CREATE OBJECT lo_rest_client
  EXPORTING
    io_http_client = lo_http_client.

lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_0 ).

*CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
* EXPORTING
* text = lv_payload
* IMPORTING
* buffer = lv_payload_x.
*

** Passing the token value in header
CALL METHOD lo_http_client->request->set_header_field
  EXPORTING
    name  = 'Authorization'
    value = lv_token_value.

** passing the content-type value in header which is a mandatory field
CALL METHOD lo_http_client->request->set_header_field
  EXPORTING
    name  = 'Content-Type'
    value = 'application/json'.

** passing the Accept value in header which is a mandatory field
CALL METHOD lo_http_client->request->set_header_field
  EXPORTING
    name  = 'Accept'
    value = 'application/json'.

lo_http_client->request->set_method( 'POST' ).
lo_http_client->request->set_content_type( 'application/json' ).
*lo_http_client->request->set_data( lv_payload_x ).
lo_http_client->propertytype_logon_popup = if_http_client=>co_disabled.

* Sending the request
lo_http_client->send(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2 ).


* Receiving the response
lo_http_client->receive(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ).


IF sy-subrc EQ 0.
ENDIF.
lv_response = lo_http_client->response->get_cdata( ).


*get the status of the response
CALL METHOD lo_http_client->response->get_status
  IMPORTING
    code   = lv_http_code
    reason = lv_http_reason.


IF sy-subrc EQ 0.
ENDIF.

In the above code i am getting the exception http_communication_failure in receive method while receiving the response. The error code is determined as 404 : Connection refused. This error is coming in both create_by_url and create_by_destination call. But this error code is not from the error code list which is provided by the client for this webservice.

Can someone please tell if the above code/method to consume single token RESTful webservice in SAP is correct? If not then what is wrong/missing in the above code. Also please comment how can I test the SM59 connection created by basis team to know if it is a valid connection or not. Currently test connection throws an error because of no username/password is maintained ( it is not applicable ! ).

Any help will be highly appreciated.

Accepted Solutions (0)

Answers (2)

Answers (2)

0 Kudos

Hi,

try to pass the token value as "Token" instead of "Authorization" in Header.

Regards,

Ranjani Sekar

0 Kudos

Hello,

did you resolve the problem ?