Calling WebService at the Unix command line

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
CalcService.sh

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.


5 Responses to “Calling WebService at the Unix command line”  

  1. 1 Kenny

    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

  2. 2 Jacob Sandin

    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
    >>

  3. 3 Luigi

    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.

  4. 4 Vinidog

    Work Very Nice!!!

    Tkx ;-)

  5. 5 ahshan md

    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

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Current day month ye@r *


Calendar

November 2008
M T W T F S S
« Oct   Dec »
 12
3456789
10111213141516
17181920212223
24252627282930

Follow me

twitter flickr LinkedIn feed

Subscribe by email

Enter your email address:

Archives


Categories

Tag Cloud


Listening