Errors while invoking Web service using `Curl` command from Unix terminal

curl

I have been trying to invoke a web service from my script using the Curl command . I got the script from HERE.

My Script :

#! /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

And i get these Errors :

./CalcService.sh: 10: ./CalcService.sh: --data: not found
./CalcService.sh: 11: ./CalcService.sh: @-: not found
./CalcService.sh: 12: ./CalcService.sh: --header: not found
./CalcService.sh: 13: ./CalcService.sh: --user-agent: not found
./CalcService.sh: 14: ./CalcService.sh: ./CalcService.sh: 14: ./CalcService.sh: http://mathertel.de/AJAXEngine/S01_AsyncSamples/CalcService.asmx: not found
 xmllint: not found

Now i have installed curl, and tried to install xmllint but it did not get installed, saying it's a broken package.

Anybody has any suggestions as to what is happening here and what i can do to mitigate this ? This is driving me crazy…

Best Answer

As @goldilocks suggested, taking out the double back slashes (\\) and replacing them with single backslashes (\) appears to fix your issue. Here's an example of the output with the modifications:

$ ./curl.bash 
<?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>

A better method?

curl provides the following construct which can be used to pass options into it:

#!/bin/bash

{ echo '--opt1'
  echo '--opt2'
  ...
} | curl --config -

So you could adapt the above like so:

#!/bin/bash

ENDPOINT="http://mathertel.de/AJAXEngine/S01_AsyncSamples/CalcService.asmx"
VALUE=1234567890

if [ -n "${1}" ]; then
    VALUE=${1}
fi

read -r -d '' DATA <<EOF
<?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

{
echo '--silent'
echo '--header "Content-Type: application/soap+xml; charset=utf-8"'
echo '--user-agent ""'
echo "--url ${ENDPOINT}"
echo '--data-binary @-'
echo "$DATA"
} | curl --config - | xmllint --format -

This construct makes it a little easier to parameterize the $DATA payloads so you could wrap the entire curl construct in a function and just pass it variables to include, for example.

Related Question