Skip to Content
0
May 30, 2023 at 01:30 PM

Issue while consuming REST API of 'sandbox.api.sap.com' from Steampunk BTP Trial account.

733 Views Last edit May 31, 2023 at 03:41 AM 5 rev

Hello Gurus,

Considering the use case of consuming an External API (OData) inside an ABAP Program from BTP Trial account, I am trying to consume a OData API from SAP Sandbox system only, the one which we can use for testing various APIs using the 'Try Out' feature there.

capture01.png

I could see somehow the call is happening successful and the number of records getting fetched is 50(in URI $top as 50 am passing) but am not able to get the fields from the response.

Am using 'xco_cp_json' for parsing the response which we get after making the call to API.

Am attaching the code:

CLASS zcl_test_class_output DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun .
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS ZCL_TEST_CLASS_OUTPUT IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
TRY.
"create http destination by url; API endpoint for API sandbox
DATA(lo_http_destination) =
cl_http_destination_provider=>create_by_url( 'https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_SALES_ORDER_SRV/A_SalesOrder?$inlinecount=allpages&$top=50' ).
"alternatively create HTTP destination via destination service
"cl_http_destination_provider=>create_by_cloud_destination( i_name = '<...>'
" i_service_instance_name = '<...>' )
"SAP Help: https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/f871712b816943b0ab5e04b60799e518.html
"create HTTP client by destination
DATA(lo_web_http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_http_destination ) .
"adding headers with API Key for API Sandbox
DATA(lo_web_http_request) = lo_web_http_client->get_http_request( ).
lo_web_http_request->set_header_fields( VALUE #(
( name = 'APIKey' value = 'JG3JeJn2WUsbu9MmO3P3ypAd5cGTxUGA' )
( name = 'DataServiceVersion' value = '2.0' )
( name = 'Accept' value = 'application/json' )
) ).
"set request method and execute request
DATA(lo_web_http_response) = lo_web_http_client->execute( if_web_http_client=>GET ).
DATA(lv_response) = lo_web_http_response->get_text( ).
CATCH cx_http_dest_provider_error cx_web_http_client_error cx_web_message_error.
"error handling
ENDTRY.
* out->write( |response: { lv_response }| ).
" Create an ABAP structure to contain the data from the API
TYPES:
Begin of ts_metadata,
id type string,
uri type string,
type type string,
etag type string,
END of ts_metadata,
Begin of ts_result_payload,
__metadata type ts_metadata,
SalesOrder type string,
SalesOrderType type string,
End of ts_result_payload,
Begin of ts_d,
__count type string,
results type standard table of ts_result_payload with empty key,
end of ts_d,
BEGIN of ts_A_SalesOrder_resp,
d type ts_d,
END Of ts_A_SalesOrder_resp.
DATA ls_A_SalesOrder_resp TYPE ts_A_SalesOrder_resp.
" Convert the data from JSON to ABAP using the XCO Library; output the data
TRY.
xco_cp_json=>data->from_string( lv_response )->apply( VALUE #(
( xco_cp_json=>transformation->pascal_case_to_underscore )
( xco_cp_json=>transformation->boolean_to_abap_bool )
) )->write_to( REF #( ls_A_SalesOrder_resp ) ).
out->write( | Data is obtained from the SAP Sandbox API for Sales Order in JSON format and read into ABAP using the XCO library. | ).
LOOP AT ls_A_SalesOrder_resp-d-results ASSIGNING FIELD-SYMBOL(<results>).
out->write( | Sales Order number { sy-tabix }: { <results>-salesorder } | ).
ENDLOOP.
out->write( | ---------------------------------- | ).
" catch any error
CATCH cx_root INTO DATA(lx_root).
out->write( lx_root->get_text( ) ).
ENDTRY.
ENDMETHOD.
ENDCLASS.

Further details:

1.) This program is available in ABAP Environment for BTP Trial account.

Requesting to guide where am going wrong here.

Thanks,

Sijin

Attachments

capture01.png (972.8 kB)