Calling WebService at the Unix command line
5 Comments Published November 11th, 2008 in Java, Linux Tags: .Recently I was implementing a web services client to integrate two different systems. To track problems and report bugs, I found quite useful to have scripts invoking the server to verify the response and reproduce the issues.
I wrote a set of bash scripts that run isolated and I used them to prototype and verify the calls.
Here is an example that invokes a public Web Service that returns the prime factors of a given number:
#! /bin/sh
ENDPOINT="http://mathertel.de/AJAXEngine/S01_AsyncSamples/CalcService.asmx"
VALUE=1234567890
if [ -n "${1}" ]; then
VALUE=${1}
fi
curl --silent \\
--data \\
@- \\
--header 'Content-Type: application/soap+xml; charset=utf-8' \\
--user-agent "" \\
${ENDPOINT} <<EOF | xmllint --format -
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<CalcPrimeFactors xmlns="http://www.mathertel.de/S01_AsyncSamples/">
<inputText>${VALUE}</inputText>
</CalcPrimeFactors>
</soap12:Body>
</soap12:Envelope>
EOF
You can specify the number to factorize at the command line, otherwise 1234567890 will be used as default.
This is a sample response that you get calling the script at the command line:
luigi@luigi-laptop:~$ ./CalcService.sh <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <CalcPrimeFactorsResponse xmlns="http://www.mathertel.de/S01_AsyncSamples/"> <CalcPrimeFactorsResult>2 3 3 5 3607 3803</CalcPrimeFactorsResult> </CalcPrimeFactorsResponse> </soap:Body> </soap:Envelope>
To execute this script you need to have curl and xmllint installed.
Search
Calendar
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Oct | Dec » | |||||
| 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 |
Archives
Categories
- Android (3)
- Apple (26)
- Books (7)
- Eclipse (14)
- Errors (3)
- Firefox (7)
- Git (2)
- Hardware (16)
- Horror Code (8)
- Internet (18)
- Java (98)
- JavaScript (9)
- Life, universe and everything (45)
- Lifehacks (25)
- Linux (50)
- Opinions (25)
- OSX (4)
- Python (1)
- Software (27)
- Speeches and Conferences (8)
- Unix (3)
- Web (21)
- Windows (19)
Tag Cloud
Android apple architecture Bash colors configuration CSS Development Düsseldorf Eclipse germany Git Google Hardware hdr How-To Java JAXB job junit Karmic Linux MacBook music night Open Source Opinion oracle OSX patterns Pitfalls Practices Resume Security Software Suspend TDD Testing tip tonemapped Tricks Ubuntu video Web XML
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.
Blog License
Blogs I like
Books on the desk
Friends' Blogs
- Antonio Terreno & Valter Bernardini
- Bruno Bossola
- Daniele Galluccio
- Domenico Ventura
- Ed Schepis
- Fabrizio Gianneschi
- Luca Grulla
- Luigi Zanderighi
- Marcello Teodori
- Mida Boghetich
- Muralidharan Chandrasekaran
- Piero Ricca
- Renzo Borgatti
- Simone Bordet
- Simone Bruno
- Uberto Barbini
- Valvolog
- Webtide blogs (Greg Wilkins & Jan Bartel)
Links




















Hi, Thank you for this example. But it doesn’t work for me. It looks like I have wrong syntex. Would you help me out? Here is what I have:
#! /bin/sh
ENDPOINT=”http://localhost/test-context-root/TESTWSSoapHttpPort”
VALUE=1234567890
if [ -n "${1}" ]; then
VALUE=${1}
fi
curl –data
@-
–header ‘Content-Type: application/soap xml; charset=utf-8′
–user-agent “”
${ENDPOINT}
1
2
5
2009
3
10
6
2009
3
18
18
GMT
EOF
thanx for script :)
However it seem the script has linefeeds in curl command wich makes it fail, I fixed this in my own tests.
Below script works for me.
${VALUE}
EOF
>>
Yes, I put the line feeds to make it readable from the div. But you are right, they should be removed in order to make it work.
Wordpress hided the \ at the end of the lines:
curl –silent \
–data \
@- \
–header ‘Content-Type: application/soap+xml; charset=utf-8′ \
–user-agent “” \
${ENDPOINT} <<EOF | xmllint –format -
You can keep the line feeds, and replace them with the \ char. If you like. Unfortunately, I just noticed this now :)
Thanks for spotting it out.
Work Very Nice!!!
Tkx ;-)
hi all
the script is not working for me….plz help
#! /bin/sh
ENDPOINT=”http://mathertel.de/AJAXEngine/S01_AsyncSamples/CalcService.asmx”
VALUE=1234567890
if [ -n "${1}" ]; then
VALUE=${1}
fi
curl –silent \
–header ‘Content-Type: application/soap xml; charset=utf-8′ \
${ENDPOINT} <<EOF | xmllint –format -
${VALUE}
EOF
ERROR :-
./CalcService.sh: 13: –header: not found
./CalcService.sh: 13: http://mathertel.de/AJAXEngine/S01_AsyncSamples/CalcService.asmx: not found
warning: failed to load external entity “–format”
-:1: parser error : Document is empty
^
-:1: parser error : Start tag expected, ‘<' not found
^
I/O error : Invalid seek