Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Calling REST API Get Method from SAP ABAP

lakshmanjk
Explorer
7,752

Hi All,

I am trying to call a REST API Get method from a Function module in SAP to convert Lat Long to Zip codes but getting error as Failure to connect to Endpoint. Please look at the below code and response to provide your inputs.

G_SERVICE: https://services-dev.XXXX.com/BingMapsREST/REST/v1/Locations/X,Y

where X & Y are Location Coordinates

Create HTTP intance using RFC destination created
* You can directly use the REST service URL as well
cl_http_client=>create_by_url(
EXPORTING
url = g_service
IMPORTING
client = g_ref_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4 ).

* If you are using cl_http_client=>create_by_url use this code to supress and pass your
* HTTP basic authenication
g_ref_http_client->propertytype_logon_popup = g_ref_http_client->co_disabled.

* Create REST client instance
CREATE OBJECT g_ref_rest_client
EXPORTING
io_http_client = g_ref_http_client.
* Set HTTP version
g_ref_http_client->request->set_version( if_http_request=>co_protocol_version_1_0 ).

IF g_ref_http_client IS BOUND AND g_ref_rest_client IS BOUND.
g_urltest = g_service.
* Set the URI if any
cl_http_utility=>set_request_uri(
EXPORTING
request = g_ref_http_client->request " HTTP Framework (iHTTP) HTTP Request
uri = g_urltest ). " URI String (in the Form of /path?query-string)
ENDIF.

* Set Payload or body ( JSON or XML)
g_ref_request = g_ref_rest_client->if_rest_client~create_request_entity( ).
g_ref_request->set_content_type( iv_media_type = if_rest_media_type=>gc_appl_json ).
* g_ref_request->set_string_data( lv_body ).

DATA: lt_fields TYPE tihttpnvp,
ls_fields TYPE ihttpnvp.

ls_fields-name = if_http_header_fields_sap=>request_uri.
ls_fields-value = g_urltest.
APPEND ls_fields TO lt_fields.

CALL METHOD g_ref_rest_client->if_rest_client~set_request_headers
EXPORTING
it_header_fields = lt_fields.

TRY.
* g_ref_rest_client->if_rest_resource~options( ).
g_ref_rest_client->if_rest_resource~get( ).

CATCH cx_root INTO g_ref_exception.
wa_errtab-type = c_e.
wa_errtab-id = c_id. "ZPM_TNA
wa_errtab-message = g_ref_exception->get_text( ).
ENDTRY.

OPTION 1:
CLEAR g_response .
g_response = g_ref_http_client->response->get_cdata( ).

OPTION 2:

g_ref_response = g_ref_rest_client->if_rest_client~get_response_entity( ).
g_response = g_ref_response->get_string_data( ).

* "Close the call
CALL METHOD g_ref_http_client->close
EXCEPTIONS
http_invalid_state = 1
OTHERS = 2.

Response:

<?xml version="1.0" encoding="UTF-8"?> -<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"> <env:Header> </env:Header> -<env:Body> -<env:Fault> -<env:Code> <env:Value>env:Receiver </env:Value> -<env:Subcode> <env:Value xmlns:fault="http://tempuri.org/soapfaults">fault:MessageBlocked </env:Value> </env:Subcode> </env:Code> -<env:Reason> <env:Text xml:lang="en">Failure connecting to endpoint: http://dev.YYY.nethttps://services-dev.XXXX.com/REST/V1/Locations/39.0637395,-94.7483701?key=AAABBBC... </env:Text> </env:Reason> <env:Detail xmlns:fault="http://tempuri.org/soapfaults" fault:type="faultDetails"> </env:Detail> </env:Fault> </env:Body> </env:Envelope>

2 REPLIES 2

enric101
Active Contributor
767

Hi,

It's possible that you have the following problems:

- Certificate error: You try to connect to https using ABAP client, it's possible that you need to create a destination with SM59 and then add ssl certificate transaction STRUST.

- API error: Could you call with browser and provide the error message?

Regards,

Enric Castella

0 Kudos
767

Hi Enric,

Thanks for your response. Actually with the browser, we are getting error 'Access Denied' as the service works only based on authentication approved by end system. I will try to create a new RFC destination with SM59 and make a call to see if it works.

I would like to know why the endpoint URL is being prefixed and the actual key is suffixed with the actual URL https://services-dev.XXXX.com/BingMapsREST/REST/v1/Locations/X,Y to give the error as 'Failure to connect to Endpoint'