Calling a Web service from an ABAP program

In this recipe, we will see how we can programmatically call a web service using the proxy class generated in the previous recipes. We will see the main steps required in coding the program.

Getting ready

For quickly creating the template code within a program, first create a new program in the ABAP editor using SE80 and open the code of the program in the right-hand side pane. Then, in the left-hand side pane navigate to the customer proxy generated under the Service Consumer node. Select the name of our consumer proxy and simply drag-and-drop from the left-hand side to the right-hand side pane (within the ABAP editor). The template code will be generated and added to your program. You may modify the code according to your requirements.

Getting ready

How to do it...

We will now dive into the details of the program:

  1. We first declare an object reference to the ZST8_CO_ZST8_MYSERVICE proxy class that is generated. We will name the reference MYPROXY. We then instantiate the object using the CREATE OBJECT statement. We also specify the logical port name, ZST8_LOGICAL_PORT, created in the previous recipe. This is written within a TRY and ENDTRY statement.
    How to do it...
  2. Then we declare our input and output structures based on the structures generated during the proxy class generation. The INPUT structure contains three fields (amount, currency, and language) corresponding to the importing parameters of our function module. Appropriate values are assigned to them.
    How to do it...
  3. Finally, we call the method ZST8_MY_SPELL_AMOUNT and pass the input values via the INPUT structure. The result of the execution is the Web service execution and is in the OUTPUT structure. For printing the words, we can print the OUTPUT-IN_WORDS-WORD.
    How to do it...

How it works...

We first declare an object based on our proxy class. Before calling the method for converting the amount into words we fill the INPUT structure with appropriate values. The two structures INPUT and OUTPUT correspond to the importing and exporting parameters of the function module, respectively. The ZST8MY_SPELL_AMOUNT method corresponds to the function module we initially used for creating the Web service. We call the method and pass the INPUT structure containing the relevant input values. The Web service is called and returns the result (amount in words) in the OUTPUT structure.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.141.115