CURLs Just Want to Have Fun

CURLs Just Want to Have FunAn intro to the command line tool cURLTimothy QuirkBlockedUnblockFollowFollowingMay 5I was recently given a timed code challenge to create a Ruby on Rails back-end that would act as a RESTful API.

In addition to all of the technical requirements, I was told that the only way that users would interact with this API was with curl.

Prior to this challenge, I had never seen nor used curl.

So I needed to quickly research and familiarize myself with curl in order to assure that my API would behave as expected.

What is CURL?Most simply, curl is a “command line tool and library for transferring data with URLs” (Curl documentation).

cURL stands for “Client URL Request Library.

” It is free, open source, and the source code can be found on GitHub/curl.

You should note that cURL as an organization actually produces two products, curl and libcurl.

The first is the command line tool I will explore in more detail, and libcurl is a development library for use in or by other programs.

How do I use CURL?I will certainly not be able to cover every method for using curl because there are around 170 variations of the curl commands.

That being said, I will provide some useful examples of how to utilize curl from the command line.

GET requestTo make a simple GET request, all one needs to do is use curl + the website URL.

For example, curl https://medium.

com will yield the following results:Please note that HTML is returned from this GET request.

You may also pass additional items along with your GET request.

For example, if a website has protected content curl allows you to pass authentication credentials.

To do so use the following syntax:curl –user "USERNAME:PASSWORD" https://www.



“USERNAME” must be replaced with your actual username in quotes.

If you do not want to include your password in your command line history, you may simply use the following request: curl –user "USERNAME" https://www.


com, and you will be prompted for your login credentials.

POST requestIn the case of my code challenge it was important that my RESTful API handled information POSTed to it correctly.

In order to POST to a website/API use the following syntax:curl –data -X POST "param1=value1&param2=value2" https://medium.

comThe –data flag does imply a POST request, and therefore you are able to leave off the -X POST flags.

The URL you are posting to goes after the data you are sending.

DELETE requestIf your API follows RESTful conventions, then you would be able to delete a particular instance by sending a DELETE request to the correct route/:id.

For example, if I had an API of schedules, and I wanted to delete one of those schedules I would use the following command: curl -X "DELETE" http://localhost:3000/schedules/:id where :id is the ID of an existing schedule.

Altering the Content-TypeStandard data sent with curl is sent using the application/x-www-form-urlencoded datatype.

The primary purpose this type of request is to send a list of name/value pairs to the server.

While this is curls default setting, you are not limited to sending data this way.

It is also not the most efficient way to send data, and you will not want to use the default if you are sending large amounts of data.

Browsers must also support sending data as multipart/form-data.

This requires some additional information, primarily MIME (Multipurpose Internet Mail Extension) headers.

Additionally, you may choose to send information as JSON.

To do so we would use the -H “Content-Type: application/json” flag to indicate we are adding a header.

An example of a POST request sent as JSON would look like the following:curl -H "Content-Type: application/json" –data '{"name": "Sample Schedule Name"}' http://localhost:3000/schedulesThe above curl POST would create a new schedule for our imaginary RESTful API, where the name of the new schedule would be: “Sample Schedule Name”.

A few fun and interesting things you can do with curlGet the weather.

 By running the command: curl http://wttr.

in/LOCATION and adding the desired location you will get the desired locations forecast.

Example of weather report returned after a curl request sent to: http://wttr.

in/NEWYORK,NYTest the status of a website.

 By running the command: curl -Is https://www.


com -L | grep HTTP/ you will be able to see the HTTP response code from the requested URL.

Example response code returned from Medium HTTP requestFind your external IP address.

 You can locate your external IP address by running the command: curl ifconfig.

me Your external IP address is different than that of your local IP address.

An external or public IP address is used across the entire Internet to locate computer systems and devices.

A local or internal IP address is used inside a private network to locate the computers and devices connected to it.

– What’s The Difference Between External And Local IP Addresses?ResourcesIf you have more than a casual interest in curl, you can find more information by referencing the curl documentation.

cURL e-book and background information: https://ec.


se/cURL manual: https://curl.



html.. More details

Leave a Reply