<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Jeff's thoughts on Software Architecture, Large Scale Services and the Technical world at large : cURL</title><link>http://blogs.msdn.com/jcurrier/archive/tags/cURL/default.aspx</link><description>Tags: cURL</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>cURL'ing up with SQL Server Data Services</title><link>http://blogs.msdn.com/jcurrier/archive/2008/04/13/curl-ing-up-with-sql-server-data-services.aspx</link><pubDate>Sun, 13 Apr 2008 09:47:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8386349</guid><dc:creator>jcurrier</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/jcurrier/comments/8386349.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jcurrier/commentrss.aspx?PostID=8386349</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jcurrier/rsscomments.aspx?PostID=8386349</wfw:comment><description>&lt;p&gt;We've begun working with some external customers (and a considerable number of internal ones) on the project and one of the first requests that came up was regarding the use of cURL.&amp;nbsp; &lt;/p&gt; &lt;p&gt;&lt;a href="http://curl.haxx.se/"&gt;cURL&lt;/a&gt;, as many of you already know, is a pretty cool command line utility which can be used to issue HTTP requests.&amp;nbsp; However we haven't, until this point, documented exactly how you can use the service from cURL.&amp;nbsp; This post will address that.&lt;/p&gt; &lt;h3&gt;Enumerating your Authority&lt;/h3&gt; &lt;p&gt;In this example I'll illustrate how you can query an authority for all of the containers that are located within it.&amp;nbsp; Before we get to this however I'll explain a bit more about how cURL functions.&amp;nbsp; &lt;/p&gt; &lt;p&gt;cURL uses various command line parameters to construct the HTTP request on your behalf.&amp;nbsp; As you might guess the -X parameter is used to specify the verb (HTTP method) that you wish to use on the request.&amp;nbsp; The -u parameter is used to specify the credentials to use and finally the last parameter is the URL you want to use.&amp;nbsp; &lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;em&gt;Command line syntax to query the authority:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;curl -X GET -u "&amp;lt;userid&amp;gt;:&amp;lt;password&amp;gt;" &lt;a href="http://&amp;lt;authority-id&amp;gt;.data.beta.mssds.com/v1/?q=''"&gt;http://&amp;lt;authority-id&amp;gt;.data.beta.mssds.com/v1/?q=''&lt;/a&gt;  &lt;p&gt;&lt;strong&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/strong&gt;  &lt;p&gt;&amp;lt;s:EntitySet xmlns:s="&lt;a href="http://schemas.microsoft.com/sitka/2008/03/&amp;quot;"&gt;http://schemas.microsoft.com/sitka/2008/03/"&lt;/a&gt; xmlns:xsi="&lt;a href="http://www.w3.org/2001/XMLSc"&gt;http://www.w3.org/2001/XMLSc&lt;/a&gt;&lt;br&gt;hema-instance" xmlns:x="&lt;a href="http://www.w3.org/2001/XMLSchema&amp;quot;"&gt;http://www.w3.org/2001/XMLSchema"&lt;/a&gt;&amp;gt;&lt;br&gt;&amp;nbsp; &amp;lt;s:Container&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Id&amp;gt;testcontainer&amp;lt;/s:Id&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Version&amp;gt;1&amp;lt;/s:Version&amp;gt;&lt;br&gt;&amp;nbsp; &amp;lt;/s:Container&amp;gt;&lt;br&gt;&amp;nbsp; &amp;lt;s:Container&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Id&amp;gt;testcontainer2&amp;lt;/s:Id&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Version&amp;gt;1&amp;lt;/s:Version&amp;gt;&lt;br&gt;&amp;nbsp; &amp;lt;/s:Container&amp;gt;&lt;br&gt;&amp;lt;/s:EntitySet&amp;gt;  &lt;h3&gt;Creating Containers and Entities&lt;/h3&gt; &lt;p&gt;As I've mentioned previously &lt;a href="http://blogs.msdn.com/jcurrier/archive/2008/03/17/some-sql-server-data-services-coding-examples.aspx"&gt;here&lt;/a&gt; we use the POST verb to create entities within SSDS.&amp;nbsp; Now, because specifying the entire payload for a POST could be cumbersome cURL allows you to specify the filename where the payload can be found.&amp;nbsp; This done use the, "--data" parameter and the, "@" symbol.&lt;/p&gt; &lt;p&gt;We'll begin with creating a container with cURL.&amp;nbsp; We'll start by creating a simple file called, "CurlContainer" containing the following XML payload:&lt;/p&gt; &lt;p&gt;&amp;lt;s:Container xmlns:s="&lt;a href="http://schemas.microsoft.com/sitka/2008/03/&amp;quot;"&gt;http://schemas.microsoft.com/sitka/2008/03/"&lt;/a&gt;&amp;gt; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Id&amp;gt;CurlContainer&amp;lt;/s:Id&amp;gt; &lt;br&gt;&amp;lt;/s:Container&amp;gt;  &lt;p&gt;Now that this is complete we can construct the command line syntax that we'll need to send the request to the service.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;em&gt;Command line syntax to create the container:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;curl -X POST --data @CurlContainer -H "Content-Type: application/xml"&amp;nbsp; -u "&amp;lt;userid&amp;gt;:&amp;lt;password&amp;gt;" &lt;a href="http://&amp;lt;authority-id&amp;gt;.data.beta.mssds.com/v1/"&gt;http://&amp;lt;authority-id&amp;gt;.data.beta.mssds.com/v1/&lt;/a&gt;&lt;/p&gt; &lt;p&gt;A couple of things that you might notice here that are different from our prior syntax.&amp;nbsp; The first is that the verb has changed (that's pretty obvious).&amp;nbsp; The second item is that we've used the, "--data" parameter to specify the payload.&amp;nbsp; We didn't need to use that before since we were only querying the service.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Finally, and perhaps most importantly, is that we specify the content type of data in the request.&amp;nbsp; This is done using the, "-H" parameter along with the header data.&amp;nbsp; &lt;u&gt;If this isn't specified then the request will be denied with a 400 so do remember to use it&lt;/u&gt;.&lt;/p&gt; &lt;p&gt;Now, when we query the authority as we did earlier I get the following output returned to me.&lt;/p&gt; &lt;p&gt;&amp;lt;s:EntitySet xmlns:s="&lt;a href="http://schemas.microsoft.com/sitka/2008/03/&amp;quot;"&gt;http://schemas.microsoft.com/sitka/2008/03/"&lt;/a&gt; xmlns:xsi="&lt;a href="http://www.w3.org/2001/XMLSchema-instance"&gt;http://www.w3.org/2001/XMLSchema-instance&lt;/a&gt;" xmlns:x="&lt;a href="http://www.w3.org/2001/XMLSchema&amp;quot;"&gt;http://www.w3.org/2001/XMLSchema"&lt;/a&gt;&amp;gt;&lt;br&gt;&lt;strong&gt;&amp;nbsp; &amp;lt;s:Container&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Id&amp;gt;CurlContainer&amp;lt;/s:Id&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Version&amp;gt;1&amp;lt;/s:Version&amp;gt;&lt;br&gt;&amp;nbsp; &amp;lt;/s:Container&amp;gt;&lt;/strong&gt;&lt;br&gt;&amp;nbsp; &amp;lt;s:Container&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Id&amp;gt;testcontainer&amp;lt;/s:Id&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Version&amp;gt;1&amp;lt;/s:Version&amp;gt;&lt;br&gt;&amp;nbsp; &amp;lt;/s:Container&amp;gt;&lt;br&gt;&amp;nbsp; &amp;lt;s:Container&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Id&amp;gt;testcontainer2&amp;lt;/s:Id&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Version&amp;gt;1&amp;lt;/s:Version&amp;gt;&lt;br&gt;&amp;nbsp; &amp;lt;/s:Container&amp;gt;&lt;br&gt;&amp;lt;/s:EntitySet&amp;gt;  &lt;p&gt;Switching to entity creation we find that not a lot has changed.&amp;nbsp; The only meaningful difference is the URL that we provide and the payload content.&amp;nbsp; Following the steps just described I can create a new entity, "CurlEntity" within the CurlContainer I just created.&amp;nbsp; Listed below is the payload I've created as well as the syntax used to create the entity.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;em&gt;Payload &lt;/em&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&amp;lt;CurlEntity xmlns:s="&lt;a href="http://schemas.microsoft.com/sitka/2008/03/&amp;quot;"&gt;http://schemas.microsoft.com/sitka/2008/03/"&lt;/a&gt; xmlns:xsi="&lt;a href="http://www.w3.org/2001/XMLSchema-instance&amp;quot; xmlns:x=&amp;quot;http://www.w3.org/2001/XMLSchema"&gt;http://www.w3.org/2001/XMLSchema-instance" xmlns:x="&lt;a href="http://www.w3.org/2001/XMLSchema&amp;quot;"&gt;http://www.w3.org/2001/XMLSchema&lt;/a&gt;"&lt;/a&gt;&amp;gt; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Id&amp;gt;CurlEntity&amp;lt;/s:Id&amp;gt; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;Name xsi:type="x:string"&amp;gt;Jeff Currier&amp;lt;/Name&amp;gt;&lt;br&gt;&amp;lt;/CurlEntity&amp;gt;  &lt;p&gt;&lt;strong&gt;&lt;em&gt;Command line syntax to create the entity:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;curl -X POST -H "Content-Type: application/xml" --data @CurlEntity.xml -u "&amp;lt;userid&amp;gt;:&amp;lt;password&amp;gt;" &lt;a href="http://&amp;lt;authority-id&amp;gt;.data.beta.mssds.com/v1/CurlContainer"&gt;http://&amp;lt;authority-id&amp;gt;.data.beta.mssds.com/v1/CurlContainer&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Finally, let's just query the container to see my new data.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;em&gt;Command line syntax to query the container:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;curl -G -u "&amp;lt;userid&amp;gt;:&amp;lt;password&amp;gt;" &lt;a href="http://&amp;lt;authority-id&amp;gt;.data.beta.mssds.com/v1/CurlContainer?q=''"&gt;http://&amp;lt;authority-id&amp;gt;.data.beta.mssds.com/v1/CurlContainer?q=''&lt;/a&gt;  &lt;p&gt;&lt;strong&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/strong&gt;  &lt;p&gt;&amp;lt;s:EntitySet xmlns:s="&lt;a href="http://schemas.microsoft.com/sitka/2008/03/&amp;quot;"&gt;http://schemas.microsoft.com/sitka/2008/03/"&lt;/a&gt; xmlns:xsi="&lt;a href="http://www.w3.org/2001/XMLSc"&gt;http://www.w3.org/2001/XMLSc&lt;/a&gt;&lt;br&gt;hema-instance" xmlns:x="&lt;a href="http://www.w3.org/2001/XMLSchema&amp;quot;"&gt;http://www.w3.org/2001/XMLSchema"&lt;/a&gt;&amp;gt;&lt;br&gt;&amp;nbsp; &amp;lt;CurlEntity&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Id&amp;gt;CurlEntity&amp;lt;/s:Id&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Version&amp;gt;1&amp;lt;/s:Version&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;Name xsi:type="x:string"&amp;gt;Jeff Currier&amp;lt;/Name&amp;gt;&lt;br&gt;&amp;nbsp; &amp;lt;/CurlEntity&amp;gt;&lt;br&gt;&amp;lt;/s:EntitySet&amp;gt;  &lt;h3&gt;Updating Entities&lt;/h3&gt; &lt;p&gt;Now that we can create and query things we need to have a way of updating them otherwise things just aren't that interesting.&amp;nbsp; We do this, as always in a RESTful service, using the PUT verb.&amp;nbsp; In this case I'm going to change the, "Name" property on my newly created entity to another name.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Like we did previously we'll create a file which will contain the payload definition for the request.&amp;nbsp; I've created this file, "CurlEntity2.xml" and will provide this on the command line.&amp;nbsp; Listed below is both the payload as well as the command line syntax for updating.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;em&gt;Update Payload:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&amp;lt;CurlEntity xmlns:s="&lt;a href="http://schemas.microsoft.com/sitka/2008/03/&amp;quot;"&gt;http://schemas.microsoft.com/sitka/2008/03/"&lt;/a&gt; xmlns:xsi="&lt;a href="http://www.w3.org/2001/XMLSchema-instance&amp;quot; xmlns:x=&amp;quot;http://www.w3.org/2001/XMLSchema"&gt;http://www.w3.org/2001/XMLSchema-instance" xmlns:x="&lt;a href="http://www.w3.org/2001/XMLSchema&amp;quot;"&gt;http://www.w3.org/2001/XMLSchema&lt;/a&gt;"&lt;/a&gt;&amp;gt; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Id&amp;gt;CurlEntity&amp;lt;/s:Id&amp;gt; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;Name xsi:type="x:string"&amp;gt;Jason Hunter&amp;lt;/Name&amp;gt;&lt;br&gt;&amp;lt;/CurlEntity&amp;gt;  &lt;p&gt;&lt;strong&gt;&lt;em&gt;Command line syntax for updating an entity:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;curl -X PUT --data @CurlEntity2.xml -H "Content-Type: application/xml" -u "&amp;lt;userid&amp;gt;:&amp;lt;password&amp;gt;" http://&amp;lt;authority-id&amp;gt;.data.beta.mssds.com/v1/CurlContainer/CurlEntity&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;em&gt;Output from listing the contents of the container&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&amp;lt;s:EntitySet xmlns:s="&lt;a href="http://schemas.microsoft.com/sitka/2008/03/&amp;quot;"&gt;http://schemas.microsoft.com/sitka/2008/03/"&lt;/a&gt; xmlns:xsi="&lt;a href="http://www.w3.org/2001/XMLSc"&gt;http://www.w3.org/2001/XMLSc&lt;/a&gt;&lt;br&gt;hema-instance" xmlns:x="&lt;a href="http://www.w3.org/2001/XMLSchema&amp;quot;"&gt;http://www.w3.org/2001/XMLSchema"&lt;/a&gt;&amp;gt;&lt;br&gt;&amp;nbsp; &amp;lt;CurlEntity&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Id&amp;gt;CurlEntity&amp;lt;/s:Id&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;s:Version&amp;gt;2&amp;lt;/s:Version&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;&amp;lt;Name xsi:type="x:string"&amp;gt;Jason Hunter&amp;lt;/Name&amp;gt;&lt;br&gt;&lt;/strong&gt;&amp;nbsp; &amp;lt;/CurlEntity&amp;gt;&lt;br&gt;&amp;lt;/s:EntitySet&amp;gt;  &lt;h3&gt;Deleteing Entities&lt;/h3&gt; &lt;p&gt;All things must come to an end even when it comes to entities.&amp;nbsp; We do this in RESTful services using the Delete verb.&amp;nbsp; Notice here that there is no need to specify a payload or a content-type header.&amp;nbsp; None of this is required as the service will determine what to delete based upon the URL.&amp;nbsp; &lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;em&gt;Command line syntax:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;curl -X DELETE -u "&amp;lt;userid&amp;gt;:&amp;lt;password&amp;gt;" &lt;a href="http://&amp;lt;authority-id&amp;gt;.data.beta.mssds.com/v1/CurlContainer/CurlEntity"&gt;http://&amp;lt;authority-id&amp;gt;.data.beta.mssds.com/v1/CurlContainer/CurlEntity&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;em&gt;Output from listing the contents of our container:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&amp;lt;s:EntitySet xmlns:s="&lt;a href="http://schemas.microsoft.com/sitka/2008/03/&amp;quot;"&gt;http://schemas.microsoft.com/sitka/2008/03/"&lt;/a&gt; xmlns:xsi="&lt;a href="http://www.w3.org/2001/XMLSchema-instance&amp;quot;"&gt;http://www.w3.org/2001/XMLSchema-instance"&lt;/a&gt; xmlns:x="&lt;a href="http://www.w3.org/2001/XMLSchema&amp;quot;/"&gt;http://www.w3.org/2001/XMLSchema"/&lt;/a&gt;&amp;gt;&lt;/p&gt; &lt;p&gt;That wraps up our cURL example for this evening.&amp;nbsp; If there are other languages or tools that you would be interested in seeing examples in please do let us know!&lt;/p&gt; &lt;p&gt;--Jeff--&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8386349" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jcurrier/archive/tags/Services/default.aspx">Services</category><category domain="http://blogs.msdn.com/jcurrier/archive/tags/SSDS/default.aspx">SSDS</category><category domain="http://blogs.msdn.com/jcurrier/archive/tags/SQL+Server+Data+Services/default.aspx">SQL Server Data Services</category><category domain="http://blogs.msdn.com/jcurrier/archive/tags/cURL/default.aspx">cURL</category></item></channel></rss>