Skip to Content

Call an External API and Parse the Response in SAP BTP ABAP Environment

Requires Customer/Partner License
Call an external API SAP BTP ABAP Environment and parse the response using JSON or XML
You will learn
  • How to connect to an external RESTful API, in this case the Overpass street map service: http://overpass-api.de/, by creating the necessary communication artifacts
  • How to work with HTTP requests in your ABAP code
  • How to query the service
  • How to parse the information in an ABAP class using either JSON and SAP’s XCO Library or using XML and a simple transformation
  • How to create an executable class and display the output in the ABAP console

Prerequisites

This tutorial enables you to work with data from an external RESTful API in your ABAP applications.

Throughout this tutorial, object names include the suffix 000. Always replace this with your group number or initials.

This tutorial was written for SAP BTP ABAP Environment. However, you should also be able to use it in SAP S/4HANA Cloud Environment in the same way.

  • Step 1
    1. In ABAP Development Tools (ADT), select your ABAP Cloud Project and choose New > ABAP Package from the context menu.

    2. Enter the following and choose Next:

      • Name = Z_API_OVERPASS_000
      • Description = Get map data using Overpass map service API
      • Add to favorite packages = Yes
      • Package type = Development
      step1a-create-package
    3. Choose Create new transport request, enter a description, such as Get data from API Overpass, then choose Finish.

      step1c-new-transport-request
  • Step 2
    1. Select your ABAP package and choose New > Other Repository Object from the context menu; then enter Outbound Service, then choose Next.

    2. Enter the following and choose Next.

      • Outbound service: Z_API_OVERPASS_OB_000
      • Description: Get data from Overpass map service
      • Service type: HTTP Service
      step2a-new-outbound-service
    3. Choose the transport request you just created, then choose Finish.

      The outbound service appears.

    4. Enter the Default Path Prefix /api/interpreter.

      step2b-outbound-service-default-path-prefix
    5. Choose Save (Ctrl + S).

  • Step 3
    1. Again, choose New > Other Repository Object from the context menu; then enter Communication Scenario.

      step3a-create-comm-scenario
    2. Enter the following and choose Next.

      • Name: Z_API_OVERPASS_CSCEN_000
      • Description: Communication Scenario: Get data from Overpass map service
      step3c-comm-scen-adt-name-etc
    3. Choose the transport request you just created, then choose Finish.

      The communication scenario appears in a new editor. Ensure the following have been entered:

      • Communication Scenario Type: Customer Managed
      • Allowed Instances: One instance per client
      step3d-comm-scen-new-editor
    4. On the Outbound tab, ensure that the authentication method Unauthenticated ticked.

    5. Add your outbound service, by choosing Add….

      step3c-add-ob
    6. Choose Browse, choose your outbound service, Z_API_OVERPASS_OB_000, then choose Finish.

      The suffix REST has added to the outbound service name automatically.

      step3d-add-ob-service-to-comm-scen
      step3e-comm-scen-editor
    7. Finally, Save your communication scenario, then choose Publish Locally.

  • Step 4

    Now, you need to create the necessary communication artifacts in Fiori Launchpad, starting with the Communication System. This artifact specifies hostname and port of the targeted remote API.

    1. In the Fiori Launchpad home page, choose Communication System, then choose New.

      step4a-comm-system
      step4b-new-comm-system
    2. Enter the following and choose Create.

      • System ID: Z_OVERPASS_000_CSYS
      • System Name: Z_OVERPASS_000_CSYS
      step4b-new-comm-system-dialog
    3. Enter host name: overpass-api.de

    4. Under Users for Outbound Communication, enter the option None.

      step4c-outbound-user-authent
      step4c-outbound-user-password-none
      step4e-users-ob-none
    5. Leave the other default settings as they are and choose Save.

  • Step 5

    A communication arrangement establishes a specific communication route by combining one communication scenario and one communication system.

    1. Go back to the Fiori Launchpad home page and choose Communication Arrangement, then choose New.

      step5a-new-comm-arr
    2. Browse to, then select your Communication Scenario, Z_API_OVERPASS_CSCEN_000 (created in ADT). The Communication Arrangement name is filled automatically.

      step5c-select-comm-scen



      step5b-comm-arr-name-etc

    3. In the main editor, select the Communication System, Z_OVERPASS_000_CSYS.
      The Service URL, port etc are filled automatically.

      step5e-comm-arr-comm-sys



      step5f-comm-arr-defaults-filled

    4. Choose Save.
      You can now check the connection.

      step5d-check-connection



      step5g-ping-successful

  • Step 6
  • Step 7

    Now, you will create an ABAP class that will call your destination, and which you can run in the console.

    1. Select your package and choose New > ABAP Class from the context menu.

      step7a-create-class
    2. Enter the following for your class and choose Next. Remember to change 000 to your group number.

      • Name: ZCL_OVERPASS_XML_000
      • Description: Get map data using Overpass + XML
      • Interfaces: IF_OO_ADT_CLASSRUN (using Add…)
      step7b-name-class
    3. Choose or create a transport request, then choose Finish.

    The skeleton class is displayed in a new editor.

    step7c-class-editor
  • Step 8
  • Step 9
    1. Check your syntax (Ctrl+F2).

    2. Format (Ctrl+F1), save (Ctrl+S) and activate (Ctrl+F3) your class.

  • Step 10

    Run your class in the console (F9). The output should look something like this.

    step10a-output-xml
    step9a-output-json
  • Step 11

    When working with ABAP for Cloud Development, what do you use to parse XML into ABAP data (and vice versa)?

  • Step 12

    Where is the target host name for an outbound HTTP call maintained?

Back to top