by Miguel Bustamante

How much can you cURL? A quick and easy intro to a useful tool.

jNh2Hditm1dzVumksHjR3y59zaSybC9RxVnX

On a good day I can flex a 20 lb weight…twice. Probably. But that’s not the type of curling we’re talking about!

Curl (or cURL), on the other hand, is a small but powerful tool for transferring files and data over URLs. On a smaller scale, it’s great for testing REST APIs. And, though most web developers might opt to use other tools such as Google’s Postman, cURL is done in the command line and can leave you feeling like a true computer hack with David Lightman-like skills (for you “War Games” fans).

CURL stands for “client” and “URL”, since it is a program run on the client side that makes HTTP requests to URL’s. Since it’s open source, you can download it here. Or if you have Gitbash already installed on your machine, it’s automatically included.

For the purposes of this quick intro, we’ll need a server that’ll allow us to make requests, and it seems JSON Placeholder fits our needs nicely. It is a fake REST API that, even though our requests won’t actually change the server’s database, will still give us the appropriate response. So go ahead and crack open that console and let’s get hacking!

Get

To start, we’ll try a simple HTTP “get” request. Scroll down to the “Resources” section in JSON placeholder and let’s take a look at the types of objects we can make a request on.

kCyy9EJv0wgDBRMWbcAqFbKHNZF8-Ux9u3z5
Objects to be requested

Nice! We can call these objects by adding “/”, then the object we want in the URL. The number on the right of the row tells us how many items we’ll get back with this request. For starters, let’s request some users. Type the following line into the console:

curl https://jsonplaceholder.typicode.com/users

You should see all ten users we were promised as JSON objects. But maybe I just want the fifth user. We’ll add “/5” after the URL to get the user with the I.D. of 5.

curl https://jsonplaceholder.typicode.com/users/5

We see the JSON object for the fifth user. Great, let’s try to post a user to the server.

Post

“Post”- ing is the process of submitting data to the server and having it be saved in the database. To do this with cURL let’s look at its options. Type:

curl --help

and you should get a bunch of cool options we can use in the terminal:

mKtjAzBrGPgeLjJRjW7fExC3SgYeUK2s8JNB
Options for cURL

For our purposes, it looks like the “-d” or “- -data” option would work nicely. If we look back at the homepage of the placeholder, in the “Routes” section, it tells us we could make a post request to “https://jsonplaceholder.typicode.com/posts”. With this information, we’ll post our own object through the console:

curl -d "title=Greatest Post Ever Written&body=Body of the Greatest post ever written" https://jsonplaceholder.typicode.com/posts

Now you’ll see the post being “created” in the db, and it has an I.D. of 101.

-zEjFQuC3q32sqCBzwxYpLwvlpsLatdXsf7h

Update

Sometimes we need to change objects in the db. We can only change things already saved in the database, and since this is a fake REST API, our post wasn’t actually saved. So lets update a post that exists. How about the 56th one. Type:

curl https://jsonplaceholder.typicode.com/posts/56

And you’ll see:

z1-dbP08TUfRBpWmLQgvybxekbAQiBCJ-T8S
Post 56

It’s saved with some funky Lorem Ipsum text that we should probably change to something intelligible. We are going to need a few other options with our command here. First, we’ll need to tell cURL that it’s a “put” request. So as we look through our “- -help” option, it seems we could use “-X” to tell cURL we want to use a “PUT” command.

Then we still want to use the “-d” option for the new data we intend to use. Let’s piece it all together. Type:

curl -X PUT -d "title=This is a new title" https://jsonplaceholder.typicode.com/posts/56

And just like that, we have changed the title of the post with the I.D. of 56 to what we wanted.

4eOxfEfuubfhDr2FQrC3zu29M4Z8hwLFJonG
New title for post 56

DELETE

And now we come to the delete. Ahh, the delete. If all else fails, destroy it all! We are going to see some of the same code as we saw in the PUT command, but all we need is to give cURL a DELETE request and the URL to the post we are to delete.

curl -X DELETE https://jsonplaceholder.typicode.com/posts/56

Notice that you get nothing back in return but a newline. Maybe on some consoles you’ll see and empty hash(“{}”). This indicates that there is nothing to return because it was deleted.

Wrapping up

We only touched on some cURL commands at a very superficial level. It is a neat tool that can be helpful when working on fully functioning API integration in your app. I would suggest looking at the manual for further reading and playing around with the different options to see what may fit your needs.