on 12-20-2014 11:10 PM
Hi,
From my ABAP program, I would like to consume an external Odata service that is currently running on an Amazon server. I am wondering if this is possible. If yes, what configuration do I need to perform on SAP system to call this OData service.
Thanks in advance.
Sunil
Sunil,
SAP Gateway is a OData producing tool and not designed for OData consuming.
To consume an OData service, here are the steps.
1. Create a RFC destination (type http) pointing to the OData provider.
2. Test the RFC destination connection to ensure that connection is successful. You may maintain authentications options as part of RFC destination. If it is a https destination (not http), then you will have to maintain SSL certificates in tcode STRUST.
3. In ABAP program, use this RFC destination and create a REST client using the REST client class (CL_REST_HTTP_CLIENT).
4. Use various REST methods from this class for GET, POST, PUT for calling the external OData service.
OSCI is a topic within Gateway which consumes an OData service, wraps it, and creates a new OData service out of Gateway. Its classes (especially /IWBEP/CL_OCI_DP) can give you sample code on how to consume a OData service.
Regards
Krishna
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Krishna,
Thank you for responding so quickly.
I tried defining an RFC destination, but the connection test is always PASS (http_status = 200) no matter what target host is provided in SM59.
Also, my program(see code below) throws an error when calling lr_json_deserializer->deserialize() method
DATA: lo_http_client TYPE REF TO if_http_client,
lo_rest_client TYPE REF TO cl_rest_http_client,
lv_url TYPE string,
lv_body TYPE string,
token TYPE string,
agreements TYPE string,
"Create a structure(or deep) that exactly matches your JSON response
abap_response TYPE ZSK01,
lo_response TYPE REF TO if_rest_entity.
* Create HTTP intance using RFC restination created
* You can directly use the REST service URL as well
cl_http_client=>create_by_destination(
EXPORTING
destination = 'ZSK01' " 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 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.
* 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
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.
Any idea what could be wrong?
Thanks
Sunil
Deserialization is a smaller problem to solve compared to connection and getting data with an external service.
When you said you always get 200, I see some problem. Did you add a invalid URL and you still get a 200?
After you execute a GET call in ABAP, you can check the response in debugger and see if you actually got data. Did you?
User | Count |
---|---|
84 | |
25 | |
12 | |
9 | |
6 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.