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:
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.
Put this into a file and save it as say req.xml.
Change the value
int to some real value like
See the following figure:
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.
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
From this, we can clearly see that the result is present as a value for node
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.
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.