Quantcast
Viewing latest article 1
Browse Latest Browse All 66

How to Get Data from Web Page using SAP ABAP?

You know that we have one of the largest SAP Technical Telegram group with more than 5300 Consultants and growing each passing day. Ask-Answer, Propose-Defend, Share & Learn is our motto. Every day numerous questions are asked, few get answered and many are left open for further research and analysis. Every day we learn something new and we always have something to ponder over.

In one of these discussions, one member had a need to read data from Web. This article is a result of the sample code snippet I wrote to help him.

This is the selection screen of my test program to read data from a Web Page.

Image may be NSFW.
Clik here to view.

I have tried to use all the new syntax of ABAP and would like the freshers in new ABAP to look into below snippets.

1. String Concatenation Operation with COND #

<code>      lv_url = |http{
        COND #(
          WHEN p_ssl EQ abap_true
          THEN |s| ) }://www.koester-consulting.com/gEtDaTaFrOmWeB.php{
            COND #(
              WHEN p_id IS NOT INITIAL
              THEN |?id={ p_id }| ) }|.</code>

Check, concatenation is done using pipe (|). Also check the COND # which can be included in the string literal operations too. When P_SSL has data then ‘s’ is concatenated after ‘http’.

New to new syntax of ABAP? Take this Free Video Course on New Features of ABAP 7.4 +

Similarly when P_ID is passed, the value of P_ID which come from selection screen is concatenated at the End.

Let’s check the Output.

1. When nothing is passed from the Selection Screen.

LV_URL = ‘ttp://www.koester-consulting.com/gEtDaTaFrOmWeB.php’.

Image may be NSFW.
Clik here to view.

2. When the P_ID is passed and P_SSL is Selected.

Image may be NSFW.
Clik here to view.

‘s’ is appended after http to make it https. Also, ‘?id=1’ is appended to the end of the URL.

LV_URL = ‘https://www.koester-consulting.com/gEtDaTaFrOmWeB.php?id=1’.

Image may be NSFW.
Clik here to view.

2. Inline Declaration in Class Method Calls

Image may be NSFW.
Clik here to view.

We need to note that, Inline Declaration is not possible for Function Module calls.

3. How to Convert JSON data to ABAP Internal Table?

<code>            /ui2/cl_json=>deserialize(
              EXPORTING
                json  = lv_data
              CHANGING
                data  = gt_customer ).</code>
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

If you don‘t know how the deep structure needs to look like, you can use the below code snippet to learn more about the received data and prepare your CHANGING structure TYPE accordingly.

<code>TRY.
DATA: lo_data TYPE REF TO data.

DATA(lo_json) = NEW /ui2/cl_json( ).

lo_json->deserialize_int(
EXPORTING
json = lv_data
CHANGING
data  = lo_data ).

CATCH cx_sy_move_cast_error .
ENDTRY.</code>

Once you have the lo_data, check it in debug mode and then re-write your code to declare the TYPE you need.

Also ReadCalculator in SAP using New ABAP Syntax

4. How to Convert ABAP Internal Table data to JSON?

<code>lv_json_body =  /ui2/cl_json=>serialize(
                  data     =  ls_abap_data
                 pretty_name = /ui2/cl_json=>pretty_mode-camel_case ) .</code>

Although serialize method was not needed in our sample report to read Web Data, but since we were talking about JSON to ABAP Internal table, I thought it would be useful if we also showed the other way around.

<code>TYPES:
  BEGIN OF ts_deep_entity,
    mat_doc    TYPE matnr,
    doc_year   TYPE gjahr,
    pstng_date TYPE sy-datum,
    ernam      TYPE sy-uname,

  END OF ts_deep_entity.

TYPES: BEGIN OF ty_head ,
         d TYPE ts_deep_entity,
         e TYPE ts_deep_entity,
       END OF ty_head.

*======================ABAP Data==================================
DATA(ls_abap_data) = VALUE ty_head(   d-mat_doc = '4900000814'  e-mat_doc = '4900000815'
                                     d-doc_year   = '2019'     e-doc_year   = '2020'
                                     d-pstng_date = '20190701' e-pstng_date = '20200701'
                                     d-ernam     =  'kuldeep'  e-ernam     =  'joshi'
                            ).
*======================ABAP to JSON==================================
DATA(lv_json_body) =  /ui2/cl_json=>serialize(
   data     =  ls_abap_data
   pretty_name = /ui2/cl_json=>pretty_mode-camel_case ) .</code>
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

If you look closely, the JSON data has been formatted in CamelCase.

Image may be NSFW.
Clik here to view.

Similarly, we can format the JSON output in different ways:

  • Camel Case
  • Extended
  • Low Case
  • None
  • User
  • User Low Case
Image may be NSFW.
Clik here to view.

We will dedicate a separate article on ABAP to JSON and JSON to ABAP format. Today, we wanted to learn a simple way to read data from Web and display in SAP ABAP. With all the above knowledge, we have written the below program. Let us test it.

Also Read A to Z of AL11 Operations

Selection Screen

Image may be NSFW.
Clik here to view.

Output 1

Image may be NSFW.
Clik here to view.

Input 2 and Output 2

Image may be NSFW.
Clik here to view.

Complete Working Code for Reference

<code>REPORT zstkoes_get_data_from_web.

TYPES:
  BEGIN OF gty_customer,
    id        TYPE string,
    firstname TYPE string,
    name      TYPE string,
    country   TYPE string,
  END OF gty_customer .
TYPES:
  gtty_customer    TYPE TABLE OF gty_customer .

CONSTANTS:
  gc_proxy_host    TYPE string VALUE 'PROXY_HOST',
  gc_proxy_service TYPE string VALUE '8090'.

DATA:
  gs_customer TYPE gty_customer,
  gt_customer TYPE gtty_customer.

SELECTION-SCREEN:
  BEGIN OF LINE,
    COMMENT 1(15) c_id.
PARAMETERS:
  p_id     TYPE string VISIBLE LENGTH 2.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN: COMMENT /1(50) c_info,
                  SKIP,
                  BEGIN OF LINE.
PARAMETERS: p_ssl TYPE xfeld.
SELECTION-SCREEN: COMMENT 3(50) c_ssl,
                  END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE.
PARAMETERS: p_proxy TYPE xfeld.
SELECTION-SCREEN: COMMENT 3(50) c_proxy,
                  END OF LINE.


*----------------------------------------------------------------------*
* INITIALIZATION.
*----------------------------------------------------------------------*
INITIALIZATION.
  c_id    = 'ID'.
  c_info  = 'Leave empty to get a list of customer.'.
  c_ssl   = 'Use SSL (HTTPS)'.
  c_proxy = 'Use Proxy'.

*----------------------------------------------------------------------*
* START-OF-SELECTION.
*----------------------------------------------------------------------*
START-OF-SELECTION.

  TRY.
      DATA: lv_url TYPE string.

      lv_url = |http{
        COND #(
          WHEN p_ssl EQ abap_true
          THEN |s| ) }://www.koester-consulting.com/gEtDaTaFrOmWeB.php{
            COND #(
              WHEN p_id IS NOT INITIAL
              THEN |?id={ p_id }| ) }|.

      cl_http_client=>create_by_url(
        EXPORTING
          url                 = lv_url
          proxy_host          = COND #(
                                  WHEN p_proxy EQ abap_true
                                  THEN gc_proxy_host    )
          proxy_service       = COND #(
                                  WHEN p_proxy EQ abap_true
                                  THEN gc_proxy_service )
          sap_username        = sy-uname
        IMPORTING
          client              = DATA(lo_client)
        EXCEPTIONS
          argument_not_found  = 1
          plugin_not_active   = 2
          internal_error      = 3
          OTHERS              = 4 ).
      IF lo_client IS BOUND.
        lo_client->send(
          EXCEPTIONS
            http_communication_failure = 1
            http_invalid_state         = 2
            http_processing_failed     = 3
            OTHERS                     = 4 ).
        IF sy-subrc NE 0.
          lo_client->get_last_error(
            IMPORTING
              message = DATA(cv_error_msg) ).
          MESSAGE cv_error_msg  TYPE 'S' DISPLAY LIKE 'E'.
          RETURN.
        ENDIF.

        lo_client->receive(
          EXCEPTIONS
            http_communication_failure = 1
            http_invalid_state         = 2
            http_processing_failed     = 3
            OTHERS                     = 4 ).
        IF sy-subrc NE 0.
          lo_client->get_last_error(
            IMPORTING
              message = cv_error_msg ).
          MESSAGE cv_error_msg  TYPE 'S' DISPLAY LIKE 'E'.
          RETURN.
        ENDIF.

        lo_client->response->get_status(
          IMPORTING
            code = DATA(lv_status) ).

        IF lv_status = 200.
          DATA(lv_data) = lo_client->response->get_cdata( ).
          IF lv_data IS INITIAL.
            MESSAGE 'No Data found' TYPE 'S' DISPLAY LIKE 'E'.
            RETURN.
          ENDIF.
* Copy JSON-Data into internal table
          IF p_id IS INITIAL.
            /ui2/cl_json=>deserialize(
              EXPORTING
                json  = lv_data
              CHANGING
                data  = gt_customer ).
          ELSE.
            /ui2/cl_json=>deserialize(
              EXPORTING
                json  = lv_data
              CHANGING
                data  = gs_customer ).
          ENDIF.
        ENDIF.

      ENDIF.
    CATCH cx_root.
  ENDTRY.

*----------------------------------------------------------------------*
* END-OF-SELECTION.
*----------------------------------------------------------------------*
END-OF-SELECTION.

* Output the data
  TRY.
      IF p_id IS NOT INITIAL.
        APPEND gs_customer TO gt_customer.
      ENDIF.
      CALL METHOD cl_salv_table=>factory
        IMPORTING
          r_salv_table = DATA(gref_salv)
        CHANGING
          t_table      = gt_customer.
      gref_salv->display( ).
    CATCH cx_salv_msg .
  ENDTRY.</code>

You may connect with me at LinkedIn or visit my company website KCO for any SAP Consulting Work.

Join our Group – Telegram SAP Technical Discuss. Install Telegram App and click this link. Ask – Answer, Propose – Defend, Share and Learn in our Motto.

Step by Step Tutorials on Web Services in SAP


Viewing latest article 1
Browse Latest Browse All 66

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>