Click here to Skip to main content
14,608,228 members

Calling SOAP WebService And Parsing The Result From Linux Shell, A Command Based Approch

Rate this:
4.90 (3 votes)
Please Sign up or sign in to vote.
4.90 (3 votes)
3 Aug 2015CPOL
Command line calling and parsing SOAP using curl and xmllint ( tested in Ubuntu 12.0, Yocto )

Introduction

Recently, I have been working in different IoT platforms like Raspberry Pi, Intel Edision and Arduino Yun. One of the common problems I faced was web service Integration. Different Linux distro supports different packages for XML parsing. So I wanted a command line solution for the same. The reason for choosing the command line is that it becomes language independent and you can call the solution using unix system calls.

I also want to keep this tutorial small and precise. Hence, I am opting for a tip/trick rather than a detailed article.

Using the Code

Here is a deployed web service:

You can test with two methods: Add and Factorial.

1. Create Soap Envelope

Once you call click on the method (here I have opted for factorial), it will display wsdl details. Copy the Soap envelope part.

Image 1

Put this into a file and save it as say req.xml.

Change the value int to some real value like 5.

See the following figure:

Image 2

2. Call the Server

curl -H 'SOAPACTION: "http://tempuri.org/Factorial"'   -X POST -H 'Content-type: text/xml' 
-d  @req.xml grasshoppernetwork.com/SSIT.asmx > result1.xml

Here result1.xml is a file where we want to redirect the output. Rest of the part is self explanatory. Just compare with the first image of the soap envelope to be absolutely sure as to what is to be replaced with what.

curl is a command line call to the server. If everything is fine, you will see the result embedded in a valid XML soap response.

Image 3

3. Format the result for better visualization

I will use xmllint as this comes ported with most of the linux (but please note that the options are not the same for all the builds).

You can get a nicely formatted result by:

xmllint --format result1.xml

Image 4

From this, we can clearly see that the result is present as a value for node FactorialResult.

4. Parse and get the value

xmllint --xpath "//*[local-name()='FactorialResult']/text()" result1.xml > a.txt

// stands for anywhere in the XML tree.

xpath is an option of xmllint for getting a node path in node document. result1.xml is the file where the request's result is redirected. text() ensures that you get only value. If you remove this, whole <FactorialResult>120<FactorialResult> will be displayed. I am also redirecting the output of this into another text file called a.txt.

I show you the result in the following figure for you to know just what to expect.

Image 5

End Note

This is a note especially for Intel Edision users using Arduino IDE. Calling HTTP from Arduino code blocks the rest of loop and is really very slow. So, you can create a bash script or a simple c program to repeatedly call and parse web service (if you are periodically logging/retrieving some data) and then read the value in the file from Arduino sketch. Make sure that in that case your script/c compiled binary must be inside /sketch folder.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Grasshopper.iics
CEO Integrated Ideas
India India
gasshopper.iics is a group of like minded programmers and learners in codeproject. The basic objective is to keep in touch and be notified while a member contributes an article, to check out with technology and share what we know. We are the "students" of codeproject.

This group is managed by Rupam Das, an active author here. Other Notable members include Ranjan who extends his helping hands to invaluable number of authors in their articles and writes some great articles himself.

Rupam Das is mentor of Grasshopper Network,founder and CEO of Integrated Ideas Consultancy Services, a research consultancy firm in India. He has been part of projects in several technologies including Matlab, C#, Android, OpenCV, Drupal, Omnet++, legacy C, vb, gcc, NS-2, Arduino, Raspberry-PI. Off late he has made peace with the fact that he loves C# more than anything else but is still struck in legacy style of coding.
Rupam loves algorithm and prefers Image processing, Artificial Intelligence and Bio-medical Engineering over other technologies.

He is frustrated with his poor writing and "grammer" skills but happy that coding polishes these frustrations.
Group type: Organisation

116 members


Comments and Discussions

 
GeneralMy vote of 5 Pin
DrABELL4-Aug-15 7:53
professionalDrABELL4-Aug-15 7:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Tip/Trick
Posted 3 Aug 2015

Tagged as

Stats

48.3K views
4 bookmarked