Sunday, 18 August 2019 14:40

How to Get Data from Web Page using SAP ABAP?

Written by https://sapyard.com/how-to-get-data-from-web-page-in-sap-abap/
Rate this item
(0 votes)

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.

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 #

Concatenate using new syntax
ABAP
1
2
3
4
5
lv_url=|http{COND#(WHENp_sslEQabap_true
THEN|s|)}://www.koester-consulting.com/gEtDaTaFrOmWeB.php{
COND#(WHENp_idISNOTINITIAL
THEN|?id={p_id}|)}|.

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’.

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

‘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’.

2. Inline Declaration in Class Method Calls

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

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

JSON to ABAP internal table
ABAP
1
2
3
4
5
6
/ui2/cl_json=>deserialize(
EXPORTING
json=lv_data
CHANGING
data=gt_customer).

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.

JSON to dynamic ABAP
ABAP
1
2
3
4
5
6
7
8
9
10
11
TRY.
DATA:lo_dataTYPEREF TO data.
DATA(lo_json)=NEW/ui2/cl_json().
lo_json->deserialize_int(
EXPORTING
json=lv_data
CHANGING
data=lo_data).
CATCHcx_sy_move_cast_errorINTODATA(lx_sy_move_cast_error).
ENDTRY.

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?

ABAP to JSON
ABAP
1
2
3
4
DATA(lv_json_body)=/ui2/cl_json=>serialize(
data=ls_abap_data
pretty_name=/ui2/cl_json=>pretty_mode-camel_case).

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.

ABAP to JSON
ABAP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
TYPES:
BEGIN OFts_deep_entity,
mat_docTYPEmatnr,
doc_yearTYPEgjahr,
pstng_dateTYPEsy-datum,
ernamTYPEsy-uname,
END OFts_deep_entity.
TYPES:
BEGIN OFty_head,
dTYPEts_deep_entity,
eTYPEts_deep_entity,
END OFty_head.
*======================ABAP Data==================================
DATA(ls_abap_data)=VALUEty_head(d=VALUE#(mat_doc='4900000814'
doc_year='2019'
pstng_date='20190701'
ernam='kuldeep')
e=VALUE#(mat_doc='4900000815'
doc_year='2020'
pstng_date='20200701'
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).

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

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

  • Camel Case
  • Extended
  • Low Case
  • None
  • User
  • User Low Case

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

Output 1

Input 2 and Output 2

Complete Working Code for Reference

Sample Report
ABAP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
REPORTzstkoes_get_data_from_web.
TYPES:
BEGIN OFgty_customer,
idTYPEstring,
firstnameTYPEstring,
nameTYPEstring,
countryTYPEstring,
END OFgty_customer.
TYPES:
gtty_customerTYPETABLEOFgty_customer.
CONSTANTS:
gc_proxy_hostTYPEstringVALUE'PROXY_HOST',
gc_proxy_serviceTYPEstringVALUE'8090'.
DATA:
gs_customerTYPEgty_customer,
gt_customerTYPEgtty_customer.
SELECTION-SCREEN:
BEGIN OF LINE,
COMMENT1(15)c_id.
PARAMETERS:
p_idTYPEstringVISIBLE LENGTH2.
SELECTION-SCREENEND OF LINE.
SELECTION-SCREEN:COMMENT/1(50)c_info,
SKIP,
BEGIN OF LINE.
PARAMETERS:p_sslTYPExfeld.
SELECTION-SCREEN:COMMENT3(50)c_ssl,
END OF LINE.
SELECTION-SCREEN:BEGIN OF LINE.
PARAMETERS:p_proxyTYPExfeld.
SELECTION-SCREEN:COMMENT3(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)=|http{COND#(WHENp_sslEQabap_true
THEN|s|)
}://www.koester-consulting.com/gEtDaTaFrOmWeB.php{
COND#(WHENp_idISNOTINITIAL
THEN|?id={p_id}|)}|.
cl_http_client=>create_by_url(
EXPORTING
url=lv_url
proxy_host=COND#(WHENp_proxyEQabap_true
THENgc_proxy_host)
proxy_service=COND#(WHENp_proxyEQabap_true
THENgc_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).
IFlo_clientISBOUND.
lo_client->send(
EXCEPTIONS
http_communication_failure=1
http_invalid_state=2
http_processing_failed=3
OTHERS=4).
IFsy-subrcNE0.
lo_client->get_last_error(
IMPORTING
message=DATA(cv_error_msg)).
MESSAGEcv_error_msgTYPE'S'DISPLAY LIKE'E'.
LEAVE LIST-PROCESSING.
ENDIF.
lo_client->receive(
EXCEPTIONS
http_communication_failure=1
http_invalid_state=2
http_processing_failed=3
OTHERS=4).
IFsy-subrcNE0.
lo_client->get_last_error(
IMPORTING
message=cv_error_msg).
MESSAGEcv_error_msgTYPE'S'DISPLAY LIKE'E'.
LEAVE LIST-PROCESSING.
ENDIF.
lo_client->response->get_status(
IMPORTING
code=DATA(lv_status)).
IFlv_status=200.
DATA(lv_data)=lo_client->response->get_cdata().
IFlv_dataISINITIAL.
MESSAGE'No Data found'TYPE'S'DISPLAY LIKE'E'.
LEAVE LIST-PROCESSING.
ENDIF.
IFp_idISINITIAL.
" Copy JSON-Data into internal table
/ui2/cl_json=>deserialize(
EXPORTING
json=lv_data
CHANGING
data=gt_customer).
ELSE.
" Copy JSON-Data into internal table
/ui2/cl_json=>deserialize(
EXPORTING
json=lv_data
CHANGING
data=gs_customer).
APPENDgs_customerTOgt_customer.
ENDIF.
ENDIF.
ENDIF.
CATCHcx_root.
ENDTRY.
*----------------------------------------------------------------------*
* END-OF-SELECTION.
*----------------------------------------------------------------------*
END-OF-SELECTION.
* Output the data
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table=DATA(gref_salv)
CHANGING
t_table=gt_customer).
CASTcl_salv_column(
gref_salv->get_columns()->get_column('ID')
)->set_medium_text('ID').
CASTcl_salv_column(
gref_salv->get_columns()->get_column('FIRSTNAME')
)->set_medium_text('Firstname').
CASTcl_salv_column(
gref_salv->get_columns()->get_column('NAME')
)->set_medium_text('Lastname').
CASTcl_salv_column(
gref_salv->get_columns()->get_column('COUNTRY')
)->set_medium_text('Country').
gref_salv->display().
CATCHcx_salv_msg.
CATCHcx_salv_not_found.
ENDTRY.

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

decode:true

Read 292 times

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.