Call an External API in SAP ABAP Environment
- How to create a new destination in SAP Cloud cockpit
- How to call an external API from inside an ABAP class by pointing to this destination
- IMPORTANT: This tutorial cannot be completed on a trial account
- You have set up SAP Cloud Platform, ABAP Environment, for example by using the relevant booster: Using a Booster to Automate the Setup of the ABAP Environment
- Tutorial: Create Your First Console Application, for a licensed user, steps 1-2
You will create a new destination service instance, specifying the URL for an external API, user/password, and authentication.
You will then create a class that calls the API and displays the output from it in the console.
This enables you to avoid hard-coding the URL of the external API in your ABAP class.
Throughout this tutorial, objects name include the suffix
XXX. Always replace this with your group number or initials.
- Step 1
You will now create a destination in the ABAP Environment. This must be created at subaccount (not Space) level.
- In the SAP Cloud Platform cockpit of your Cloud Foundry subaccount, navigate to your subaccount.
Choose Destinations, then choose New Destinations.
Enter the following values and choose Save:
Field Name Value Name e.g.
Description Can be anything, here
https://www.openstreetmap.org/#map=18/49.29271/8.64401(In your browser, this would display
Proxy Type Internet Authentication
Choose Check Connection. You should get the following response.
- Step 2
In ABAP Development Tools (ADT), select the ABAP Cloud Project and choose New > ABAP Package.
Enter the following and choose Next:
- Name =
- Description = Call external API
- Package type = Development
- Name =
Choose Create new transport request, enter a description, such as “External API”, then choose Finish.
- Step 3
Now, you will create an ABAP class that will call your destination, and which you can run in the console.
In the ABAP Development Tools
(ADT), in the Package Explorer, select your package and choose New > ABAP Class from the context menu.
Enter a name and description for your class and choose Next. Remember to change
XXXto your group number.
Choose or create a transport request, then choose Finish.
The class is displayed in a new editor.
- Step 4
Add the following
interfacesstatement to the public section:ABAPCopy
PUBLIC SECTION. INTERFACES if_oo_adt_classrun. PRIVATE SECTION.
This enables you to run the class in the console.
- Step 5
To be able to access the external service, you must:
- maintain a cloud destination for the remote system
- create an HTTP client
- send an HTTP request
- output the response in the ABAP console
- wrap the code in an exception
Copy the following code. Replace the
i_namewith your group number:ABAPCopy
METHOD if_oo_adt_classrun~main. TRY. DATA(lo_destination) = cl_http_destination_provider=>create_by_cloud_destination( i_name = 'Z_STREETMAP_XXX' i_authn_mode = if_a4c_cp_service=>service_specific ). DATA(lo_http_client) = cl_web_http_client_manager=>create_by_http_destination( i_destination = lo_destination ). DATA(lo_request) = lo_http_client->get_http_request( ). DATA(lo_response) = lo_http_client->execute( i_method = if_web_http_client=>get ). out->write( lo_response->get_text( ) ). CATCH cx_root INTO DATA(lx_exception). out->write( lx_exception->get_text( ) ). ENDTRY. ENDMETHOD.
- Step 6
- Check your syntax (
- Save (
Ctrl+S) and activate (
Ctrl+F3) your class.
- Check your syntax (
- Step 7
Run your class in the console (
The output should look something like this:
- Step 8
Which of the following is code snippets is correct? Choose all that apply.