Consuming SOAP web services with CURL

Recently, I had to consume a SOAP web service via SSH using curl because a direct connection between my laptop and the web service endpoint was not allowed.

In this post, I am going to show you how to invoke a public SOAP web service named GeoIPService via SSH using curl.

1. Write the request into a XML file named request.xml using 8.8.8.8 as IP address.

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Body>
      <GetGeoIP xmlns="http://www.webservicex.net/">
         <IPAddress>8.8.8.8</IPAddress>
      </GetGeoIP>
   </soap:Body>
</soap:Envelope>

2. Invoke the web service using request.xml file as data-binary as well as the header SOAPAction

 curl -i -X POST -H "Content-Type: text/xml" -H "SOAPAction: \"http://www.webservicex.net/GetGeoIP\"" --data-binary @request.xml "http://www.webservicex.net/geoipservice.asmx"

3. And then you will have received something like this

HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/7.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 05 May 2017 11:50:56 GMT
Content-Length: 517
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Body>
      <GetGeoIPResponse xmlns="http://www.webservicex.net/">
         <GetGeoIPResult>
            <ReturnCode>1</ReturnCode>
            <IP>8.8.8.8</IP>
            <ReturnCodeDetails>Success</ReturnCodeDetails>
            <CountryName>United States</CountryName>
            <CountryCode>USA</CountryCode>
         </GetGeoIPResult>
      </GetGeoIPResponse>
   </soap:Body>
</soap:Envelope>

And that's it! All you need to do to invoke a SOAP web service with curl is:

  • Set the header Content-Type to text/xml
  • Set the header SOAPAction
  • Set the request as a xml file