Providing Enterprise Data Simply via SAP BSP - Handling URL Parameters


Overview

In the previous posts, we built a very simple BSP application that we used to retrieve a simple, hard-coded JSON. Then we went ahead, and we replaced this hard-coded JSON in code using a simple SELECT statement, and a JSON transformation.

Today, we move on and talk about how to pass parameters to our web service in order to pass a filtered data back to the client. We are going to solve these requirements using Page Attributes.

Today lessons

How to pass parameters to the service


Add New Page Attributes

Today we are going to work on the tab, Page Attributes.


Here, we are going to list the parameters that we want to use to filter the flight schedules. For the sake of the simplicity, let's define one parameter for the Departure and one for the Arrival City. To declare them, simply let's use the same data elements as in the table, SPFLI (S_FROM_CIT, S_TO_CITY).

In order to get the parameters from the calling URL, we must check the option, Auto for both two parameters. (Note: without this checkbox, our attributes: P_CITYFROM and P_CITYTO stay empty).


Modify Data Extraction

Instead of using these parameters to filter the records in our SELECT statement, first we are going to wrap these parameters into two RANGEs. This is because, if we won't get any parameter from the client in the URL, then we will retrieve all the records in the table.

DATA range_cityfrom TYPE RANGE OF s_from_cit.
DATA range_cityto   TYPE RANGE OF s_to_city.

SELECT * FROM spfli INTO TABLE @DATA(flight_schedules) UP TO 2 ROWS.

DATA(json_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).

TRY.
  CALL TRANSFORMATION id
    SOURCE flightschedules = flight_schedules
    RESULT XML json_writer.

  CATCH cx_xslt_format_error INTO DATA(xslt_format_error).
    " error handling
ENDTRY.

CALL METHOD response->if_http_entity~set_data
  EXPORTING
    data = json_writer->get_output( ).

To fill up the ranges, we are going to use the well-know VALUE constructor expression, where we simply fill the SIGN, OPTION and LOW fields up with the following values (if the given parameter is empty, then we don't initialize the range neither):

DATA range_cityfrom TYPE RANGE OF s_from_cit.
DATA range_cityto   TYPE RANGE OF s_to_city.

IF p_cityfrom IS NOT INITIAL.
  range_cityfrom = VALUE #(
    (
      sign   = 'I'
      option = 'EQ'
      low    = p_cityfrom
    )
  ).
ENDIF.

IF p_cityto IS NOT INITIAL.
  range_cityto = VALUE #(
    (
      sign   = 'I'
      option = 'EQ'
      low    = p_cityto
    )
  ).
ENDIF.

SELECT * FROM spfli INTO TABLE @DATA(flight_schedules) UP TO 2 ROWS.

DATA(json_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).

TRY.
  CALL TRANSFORMATION id
    SOURCE flightschedules = flight_schedules
    RESULT XML json_writer.

  CATCH cx_xslt_format_error INTO DATA(xslt_format_error).
    " error handling
ENDTRY.

CALL METHOD response->if_http_entity~set_data
  EXPORTING
    data = json_writer->get_output( ).

At last, we simply replace the UP TO 2 ROWS addition to a WHERE clause with our ranges.

DATA range_cityfrom TYPE RANGE OF s_from_cit.
DATA range_cityto   TYPE RANGE OF s_to_city.

IF p_cityfrom IS NOT INITIAL.
  range_cityfrom = VALUE #(
    (
      sign   = 'I'
      option = 'EQ'
      low    = p_cityfrom
    )
  ).
ENDIF.

IF p_cityto IS NOT INITIAL.
  range_cityto = VALUE #(
    (
      sign   = 'I'
      option = 'EQ'
      low    = p_cityto
    )
  ).
ENDIF.

SELECT * FROM spfli INTO TABLE @DATA(flight_schedules)
  WHERE cityfrom IN range_cityfrom AND
        cityto   IN range_cityto.

DATA(json_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).

TRY.
  CALL TRANSFORMATION id
    SOURCE flightschedules = flight_schedules
    RESULT XML json_writer.

  CATCH cx_xslt_format_error INTO DATA(xslt_format_error).
    " error handling
ENDTRY.

CALL METHOD response->if_http_entity~set_data
  EXPORTING
    data = json_writer->get_output( ).


Test Your BSP Application

Now comes the best part of building something ... of course running the app itself! For this, simply let's right-click on your BSP application and choose the option, Test! or let's paste the following URL into your browser:

http://xxxx:xxxx/sap/bc/bsp/sap/zflights/schedules

From now on, we can play with the parameters freely:

http://xxxx:xxxx/sap/bc/bsp/sap/zflights/schedules?p_cityfrom=NEW YORK or

http://xxxx:xxxx/sap/bc/bsp/sap/zflights/schedules?p_cityto=FRANKFURT or

http://xxxx:xxxx/sap/bc/bsp/sap/zflights/schedules?p_cityfrom=NEW YORK&p_cityto=FRANKFURT


Summary

Now, you have a ready-to-use, lightweight web service in SAP that you can use easily to consume some data from SAP, for example in a nice Xamarin app.

Have you got the JSON message again after running your BSP app? I hope, you could follow me, and everything went fine. If not, then don't hesitate to leave me a comment below!


blog comments powered by Disqus