Skip to Content

Get Data from an External API in SAP BTP ABAP Environment Using JSON

Requires Customer/Partner License
Call an external API SAP BTP ABAP Environment and display its output in the console using JSON
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 query the service for a specific amenity in a specific city, e.g. Heidelberg beer gardens.
  • How to parse the information in an ABAP class using JSON and SAP’s XCO Library
  • How to display the information in the ABAP console
    This tutorial enables you to work with XML 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.
julieplummer20Julie PlummerNovember 24, 2022
Created by
julieplummer20
November 24, 2022
Contributors
julieplummer20

Prerequisites

  • 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
    3. Choose the transport request you just created, then choose Finish.

      The communication scenario appears.

      step3b-new-comm-scen
    4. On the Outbound tab, ensure that all four authentication methods are 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 the URL of the API (minus the HTTP(S) protocol) and port.

    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

       
      step4d-outbound-user-authent-none

       
      step4e-users-ob-none

    5. Leave the other default settings as they are and choose Save.

  • Step 5

    This next artifact specifies the communication scenario and 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

    You can now check the connection.

    step5d-check-connection

     

    step5g-ping-successful

  • Step 6

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

    1. In the ABAP Development Tools (ADT), in the Package Explorer, select your package and choose New > ABAP Class from the context menu.

      step4a-create-class
    2. Enter a name and description for your class. Remember to change 000 to your group number.

      • Name: zcl_api_overpass_json_000
      • Description: Get map data using Overpass + JSON
      step4b-class-name
    3. Add the interface IF_OO_ADT_CLASSRUN by choosing Add….

      step4c-add-interface

       

      step4e-class-plus-interface

    4. Choose or create a transport request, then choose Finish.

    The class is displayed in a new editor.

    step4d-class-editor
  • Step 7

    To be able to access the external service, you must:

    • create an HTTP client that points to your communication scenario and service id
    • send an HTTP request
    • output the response in the ABAP console
    • wrap the code in an exception

    Copy the following code. Replace 000 with your group number or initials.

    ABAP
    Copy
    METHOD if_oo_adt_classrun~main.
    
      " Create HTTP client; send request
      TRY.
      DATA(lo_destination) = cl_http_destination_provider=>create_by_comm_arrangement(
                               comm_scenario  = 'Z_OVERPASS_JSON_CS_000'
                               service_id     = 'Z_API_OVERPASS_JSON_000_REST'
    
                             ).
    
          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( ).
    
          lo_request->set_query( query = 'data=[out:json];area[name="Heidelberg"];node["amenity"="biergarten"](area);out;' ).
    
          DATA(lo_response) = lo_http_client->execute( i_method = if_web_http_client=>get ).
          DATA(lv_json_results) = lo_response->get_text( ).
    
        CATCH cx_root INTO DATA(lx_exception).
          out->write( lx_exception->get_text( ) ).
      ENDTRY.
    
      " Create an ABAP structure to contain the data from the API
      TYPES:
        BEGIN OF ts_tags,
          name    TYPE string,
          website TYPE string,
        END OF ts_tags,
    
        BEGIN OF ts_element,
          type TYPE string,
          id   TYPE string,
          lat  TYPE string,
          lon  TYPE string,
          tags TYPE ts_tags,
        END OF ts_element,
    
        BEGIN OF ts_osm3s,
          timestamp_osm_base  TYPE string,
          timestamp_areas_base TYPE string,
          copyright TYPE string,
        END OF ts_osm3s,
    
        BEGIN OF ts_osm,
          version   TYPE string,
          generator TYPE string,
          osm3s     TYPE ts_osm3s,
          elements  TYPE STANDARD TABLE OF ts_element WITH EMPTY KEY,
        END OF ts_osm.
    
      DATA ls_osm TYPE ts_osm.
    
      " Convert the data from JSON to ABAP using the XCO Library; output the data
      TRY.
    
          xco_cp_json=>data->from_string( lv_json_results )->apply( VALUE #(
            ( xco_cp_json=>transformation->pascal_case_to_underscore )
            ( xco_cp_json=>transformation->boolean_to_abap_bool )
          ) )->write_to( REF #( ls_osm ) ).
    
          out->write( | Names of beergardens in Heidelberg (Germany) found on OpenStreetMaps. | ).
          out->write( | Data is obtained from the OSM API in JSON format and read into ABAP using the XCO library. | ).
          out->write( | Note: Not all locations have an associated name. | ).
          out->write( | Generator: { ls_osm-generator } | ).
          out->write( | ---------------------------------- | ).
    
          LOOP AT ls_osm-elements ASSIGNING FIELD-SYMBOL(<element>).
            out->write( | Beergarden number { sy-tabix }: { <element>-tags-name } | ).
          ENDLOOP.
          out->write( | ---------------------------------- | ).
    
        " catch any error
        CATCH cx_root INTO DATA(lx_root).
          out->write( lx_root->get_text( ) ).
      ENDTRY.
    
    
    ENDMETHOD.
    
    
  • Step 8
    1. Check your syntax (Ctrl+F2).
    2. Format (Ctrl+F1), save (Ctrl+S) and activate (Ctrl+F3) your class.
  • Step 9

    Run your class in the console (F9).

    The output should look something like this:
     

    step9a-output

  • Step 10
  • Step 11
Back to top