0% found this document useful (0 votes)
80 views

Simple Code To Consume Web Service Using SAP ABAP

This code uses ABAP to consume a web service that returns cities by country. It calls the web service, receives the XML response, and processes the XML string by finding city names between XML tags and writing them to the screen. The code demonstrates consuming a web service and parsing an XML response. However, the document notes that newer ABAP provides the CALL TRANSFORMATION command for more efficient XML processing using defined transformations.

Uploaded by

viceawork01
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Simple Code To Consume Web Service Using SAP ABAP

This code uses ABAP to consume a web service that returns cities by country. It calls the web service, receives the XML response, and processes the XML string by finding city names between XML tags and writing them to the screen. The code demonstrates consuming a web service and parsing an XML response. However, the document notes that newer ABAP provides the CALL TRANSFORMATION command for more efficient XML processing using defined transformations.

Uploaded by

viceawork01
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Simple Code to consume Web service using SAP

ABAP
REPORT zpw_webservice.

*&---------------------------------------------------------------------*
*& Selection Screen
*&---------------------------------------------------------------------*
PARAMETERS : p_cnt TYPE t005t-landx .

*&---------------------------------------------------------------------*
*& Types and Data
*&---------------------------------------------------------------------*
DATA: http_client TYPE REF TO if_http_client ,
http_url TYPE string ,
p_content TYPE string .

*&---------------------------------------------------------------------*
*& Start of Selection
*&---------------------------------------------------------------------*
START-OF-SELECTION .

* Build the url string based on input


CONCATENATE 'http://www.webservicex.net/globalweather.asmx'
'/GetCitiesByCountry?CountryName='
p_cnt
INTO http_url .

* Creation of new IF_HTTP_Client object


CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = http_url
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.

http_client->request->set_header_field( name = '~request_method'


value = 'GET' ).
* Send the request
http_client->send( ).

* Reterive the result


CALL METHOD http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state =2
http_processing_failed = 3
OTHERS = 4.

p_content = http_client->response->get_cdata( ).
REPLACE ALL OCCURRENCES OF '&lt;' IN p_content WITH '<' .
REPLACE ALL OCCURRENCES OF '&gt;' IN p_content WITH '>' .

*&---------------------------------------------------------------------*
*& Processing string
*&---------------------------------------------------------------------*
DATA : moff TYPE syst-tabix ,
moff1 TYPE syst-tabix ,
len TYPE syst-tabix .

DO .
FIND '<City>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET
moff .
IF sy-subrc = 0 .
moff = moff + 6 .
FIND '</City>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH
OFFSET moff1 .
len = moff1 - moff .
WRITE : / p_content+moff(len) .
ELSE.
EXIT.
ENDIF.
ENDDO .
In this code processing of XML string is done using FIND statement. SAP has introduced command CALL
TRANSFORMATION since Basis release 6.10 for XML processing. This command CALL TRANSFORMATION
process the whole XML string based on defined transformation and return it in variable/internal table.
Transformations can be defined using transaction SE80. Ok I am leaving this topic for next blog ;)

You might also like