Testing .Net Webservices with cURL

Home/.Net/Testing .Net Webservices with cURL

Fair warning, this is one of those posts that may or may not be useful to many of you out there, but I wanted to document this somewhere ’cause there’s a good chance I’ll need to do it again in the near future.

I’m wrapping up a mobile application that needed to communicate with a couple .Net WSDL web services for retrieving lookup data and a webmethod that allows the user to submit an xml document to a .Net application server. The services themselves were previously created by the client’s development team and I’m consuming them through a Ruby-based mobile platform (Rhomobile) which doesn’t support WSDL out-of-the-box but does allow async HTTP calls, but I was having trouble getting the request body just right and decided to use cURL to test my logic before trying in within the app.

Ex 1 – A simple call to retrieve a list of data.
For this scenario, testing was actually pretty easy:

curl -d "User=jmiller&MaxRecords=10" http://somewebsite.com/calendar.asmx/ListAppointments

The “-d” (or –data) switch used above allows us to set parameter values we want to send the webservice.

Ex. 2 – Post custom arguments that include an XML document to web method.
This case was a little more complicated, I needed to create the actual SOAP envelope and nest an XML document within it. Navigating to the web service in a browser will give the template that the body needs to be in; I created a text file “servicetest.xml” that included that template with actual values populated within it.


<?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">
    <UpdateTask xmlns="http://somewebsite.com/Calendar/">
      <TaskXml>…..some xml….</TaskXml>

And the cURL syntax:

curl -d @servicetest.xml -H "Content-Type: application/soap+xml" http://somewebsite.com/Calendar.asmx?op=UpdateTask

We’re using the “-d” switch again but this time we’re passing in our xml file that includes the SOAP body as a parameter (including the “@” indicates that it’s a file rather than just a string). The “-H” switch allows us to include a header that specifies the content type as SOAP Xml. You can include many more headers, including user agent. Last, notice that we had to modify the URL slightly in this scenario.

For anyone new to it, cURL is a handy little tool that really helps simplify a lot of basic tasks. Check out the man page for all the options.

Tags: |

About the Author:

Freelance software developer in the Cleveland area.

Leave A Comment