In one of my previous article i had shown how to perform CRUD operations on an OData using a new JavaScript library/API from Microsoft called dataJS. With that example we saw how from a HTML page we can perform the CRUD operations on a OData service. With just dataJS and JQuery we were able to build a complete application. Here is the article link if you have not read it: Performing CRUD on OData Service using DataJS

In this post i will explore the feasibility of performing the CRUD operations on a OData service but from a command line. Yes, from a command line we can perform the CRUD operations to a OData service. For the rest of this article, i will be using the same service that i had built in “Performing CRUD on OData Service using DataJS”. You would need to follow Step 1 to 7 to get an OData service up and running. So lets start looking into what is cURL and how do we perform CRUD operation using cURL.

What is cURL?

Here is the official definition for what is cURL:

curl is a command line tool for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos…), file transfer resume, proxy tunneling

So cURL is a command line tool for transferring data with URL syntax. And supports many different protocol.

How will cURL help in CRUD operations on OData?

If you remember the tenets of OData, we communicate to any OData service using HTTP and we use the HTTP verbs like GET, POST, PUT, DELETE to indicate the actions to be performed to the service. Also the content negotiation happens through the HTTP headers. cURL also provides us the ability to pass the headers and the HTTP verbs right from the command line. we will see the examples one by one.

Where do we start?

Follow Step 1 to Step 7 on my previous blog post “Performing CRUD on OData Service using DataJS”. You can also download the source and set up the OData Service from the attached demo code on that blog post.

Also download the cURL library from the official site: http://curl.haxx.se/download.html. They support plethora of platforms. Select your platform and download the right version of cURL. You can also go through cURL download wizard here http://curl.haxx.se/dlwiz/ which will guide you through the right version for you by asking certain question.

The download is a single executable by name curl.exe.

Pre Step:

Open a command prompt and navigate to the folder where you have downloaded the cURL library. in my case here is what it looks like:

curl

Performing READ operation:

Assuming you have done the Pre Step, lets now jump into some action. First and foremost lets see how to do a GET using cURL. OData service in my system is running at http://localhost:57581/UsersOData.svc/Users. So this is the service i will be using for all the operations.

cURL supports the following syntax for doing a GET:

curl.exe <url>

Thats it. curl.exe takes a URL, performs HTTP get and will return the HTTP response right on the console. So in our case the command i issued was as follows:

curl.exe http://localhost:57581/UsersOData.svc/Users

Here is the snapshot of the result:

curl-get

Performing CREATE operation:

When we want to create a resource, we would need to do the following:

  • First and foremost, the data that needs to be transferred i.e. atom/xml or JSON
  • Second, what format is the data we are trying to transfer i.e. HTTP Header
  • Third and important one, the HTTP verb which in this case is POST

cURL has certain command line options to achieve all the above:

  • – d, –data :- option which is used to set the data we are passing
  • -H, –header :- option which is used to set the Header for the request
  • -X, –request – option which is used to set the HTTP Verb or the request method

So here is the curl command for create a request:

curl.exe

-d “{username:’Lohith’,email:’lohith@test.com’,password:’pass’}”

-H content-type:application/json

-X POST

http://localhost:57581/UsersOData.svc/Users

If all went well you will see the following response on the command line:

curl-post

That’s it. We created a resource and we got the response from the server. And the response is actually a entry item of the newly created resource. Pay attention to the userid column. That’s the identity of the newly created resource

Performing UPDATE operation:

Very similar to CREATE request. But when you want to edit a resource the change is the URL and the method, everything else remains the same. The URL would need to be pointing to that resources rather than the collection. And the method will be PUT. Here is the command for UPDATE:

curl.exe

-d “{username:’Lohith Ed’,email:’lohith@test.com’,password:’pass123’}”

-H content-type:application/json

-X PUT

http://localhost:57581/UsersOData.svc/Users(15)

Notice the usage of the URL – Users(15). In OData this is the addressability factor of any resource. It says find a resource whose id is 15 in the users collection.

For a PUT request, server wont return back anything if the request succeeded.

curl-put

Performing DELETE operation:

DELETE operation is very similar to UPDTE operation. Changes are in the request method we pass and the data. For a DELETE operation method passed will be “DELETE” and we wont pass any data. The URL needs to point to the resource we want to delete. Here is the command:

curl.exe

-X PUT

http://localhost:57581/UsersOData.svc/Users(15)

That’s it. We have deleted the resource with id 15. If the command was successful, you wont get any return data from server similar to UPDATE.

So this was a walkthrough of how Open Data Protocol makes it possible for any clients having the capability to communicate via HTTP, interact with OData service and perform CRUD operation.

Hope this post was useful. Till next time, as usual Happy Coding. Code with passion, Decode with patience.

  • vizzini

    Great post — it saved me a lot of time.  Thanks! 

    One comment: in the “Performing DELETE operation”, shouldn’t the verb passed to curl.exe be DELETE?  In other words, “-X DELETE http://…” instead of “-X PUT http://…”.

    • Hi Vizzini

      My bad. Thanks for pointing it out. its Ctrl+C and Ctrl+V disaster :). will corect the same. Thanks for pointing this out. Glad this helped you.

  • Good job, I use cURL for testing SOAP, REST service and in PHP to get through proxy servers to inner network. You are tireless in seeking knowledge in .NET programming!