How to start using Curl and why: a hands-on introduction (2024)

/ #Web Development
How to start using Curl and why: a hands-on introduction (1)

by Luciano Strika

How to start using Curl and why: a hands-on introduction (2)

Whether it’s testing the output of an API before deploying it to production, or simply fetching a response from a website (for instance, to check it’s not down), Curl is practically omnipresent.

As a Data Scientist I’ve had to use it from time to time. However, more often than not I ended up just replacing parameters from a copied and pasted curl command that went around my team’s Slack channel.

I decided I needed to understand this powerful tool better if I wanted to use it to its full potential, and now I’m here to share some of the most interesting things I found in this curl tutorial.

If you have any tips or tricks you’d like to add, please do so in the comments, as my understanding of this tool is still in its early stages.

Curl: What is it good for?

Curl is a command-line tool that allows us to do HTTP requests from shell. It also covers many other protocols, like FTP, though they go beyond the scope of this tutorial.

Its name stands for “Client URL”, and it was developed by Swedish developer Daniel Stenberg. It is an open source project, and its code can be found here, in case you feel like contributing.

You can invoke it from your favorite terminal, and it usually comes pre-installed in Linux-based OS’s. Otherwise, it can normally be downloaded through apt-get on Linux, and brew on Mac.

Calling a GET method

In its most basic form, a curl command will look like this:

curl http://www.dataden.tech

The default behavior for curl is to invoke an HTTP GET method on the given URL. This way, the program’s output for that command will be the whole HTTP response’s body (in this case, HTML) the site returns on a GET, which will be written as given on stdout.

If you wish to read through a response without leaving the shell, I’d recommend at least piping it into a less command, to be able to easily scroll through the output.

Many times we’ll wish to direct the response’s contents into a file. This is done with the -o argument, like this:

curl -o output.html www.dataden.tech

which is equivalent to:

curl www.dataden.tech > output.html

Optionally, you can specify the URL of the site you wish to call curl on with a -s argument, like this:

curl -s http://www.dataden.tech

allowing you to change the order of your arguments.

You can also use –next to specify more than one URL, though the official doc advises to instead call curl on each URL in a different command.

Doing a POST to a URL

Sometimes you’ll want to test whether an API is working correctly, and usually that will require sending arguments to it.

We’ll usually do this through the POST method, passing some JSON with all the required parameters. There are many ways to do this with curl.

You can pass your arguments’ values like this:

curl --data "name=John&surname=Doe" http://www.dataden.tech

Or like a regular JSON:

curl --data '{"name":"John","surname":"Doe"}' \http://www.dataden.tech

Using –data is equivalent to using -d, and both will make the method change to POST automatically. However, we can also use the -X flag (–request) to specify which method we want to invoke:

curl -X "POST" \-d "name=John&surname=Doe" http://www.example.com

Fetching the site’s headers

Sometimes we just need to quickly see if the site’s still up, without really wanting to load the whole, potentially heavy, response. Other times, the headers store important configurations.

Those two use cases are also addressed by curl. We can use the –include (-i) parameter to include the headers, and –head (-I -that’s capital ‘i’-) to include only the headers (calling the HEAD method).

Setting your user-agent value

Now that I’ve covered the basics, let me walk you through some of the coolest things I’ve found we can do with curl.

The user-agent argument lets you specify which device and browser versions you are using, in case that makes the site render differently.
With this, you could see the mobile version of a site from your laptop, or vice versa.

From a security standpoint, this probably raises some issues. I didn’t know how easy it is to pretend to be using a different device (without even using a virtual machine) until now, and working in Fraud Prevention I can see why this could be a problem.

With that said, as long as you are using this for good, this is an awesome way of seeing what a website looks like from a tablet, a mobile device or a laptop, to name a few.

Here’s an example, straight from the official documentation (though lists of user-agents are readily available online).

curl --user-agent "Mozilla/4.73 [en] (X11; U; Linux 2.2.15 i686)" www.example.com

Timing a connection with Curl

Another reason I started learning more about curl was that I wanted to see how long exactly it took for my website to respond.

Though the basic documentation doesn’t cover it, a bit of googling uncovered this command, which I’ve found very useful:

curl -w "%{time_total}\n" -o /dev/null -s www.example.com

This will simply output the total time it took to fetch the response from the given domain.

More generally, the -w (–write-out) argument takes a special formatting string, and fills in reserved keywords with different properties of the response, in a formatted way. All keywords, and their respective values, are available in the command’s man page.

Further reading

Here are a few links you may find interesting, in case you wish to learn more about this broad subject:

To conclude

I hope you’ve found this introduction useful, and you leave this tutorial knowing at least the basics of this convenient command.

As I said before, I’m still learning as well, and will appreciate any other interesting bits of knowledge about the program’s use. The same goes for any feedback on what I’ve written so far.

If I’ve made any mistakes, or there’s any part you think I could have worded more clearly, please let me know.

I hope I’ll see you again soon, happy coding!

Follow me on Medium and Twitter to keep up to date with my tutorials, tips and articles. Consider sharing this article with a web developer friend if you liked it (or as a passive aggressive way of telling them to learn curl).

Originally published at www.dataden.tech on October 7, 2018.

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

If this article was helpful, .

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTIsem*nT

How to start using Curl and why: a hands-on introduction (2024)
Top Articles
Best MTG Arena decks 2024
U-Haul Dealer Average Income & Benefits (2024) - Vetted Biz
COLA Takes Effect With Sept. 30 Benefit Payment
Call Follower Osrs
Pj Ferry Schedule
Tanger Outlets Sevierville Directory Map
Red Heeler Dog Breed Info, Pictures, Facts, Puppy Price & FAQs
Phillies Espn Schedule
Sams Gas Price Fairview Heights Il
How to watch free movies online
Void Touched Curio
Napa Autocare Locator
Skyward Login Jennings County
Comics Valley In Hindi
Roll Out Gutter Extensions Lowe's
Aspen Mobile Login Help
Persona 4 Golden Taotie Fusion Calculator
Dover Nh Power Outage
Music Go Round Music Store
Between Friends Comic Strip Today
Stoney's Pizza & Gaming Parlor Danville Menu
Baja Boats For Sale On Craigslist
Japanese Mushrooms: 10 Popular Varieties and Simple Recipes - Japan Travel Guide MATCHA
Academy Sports Meridian Ms
Horn Rank
Anonib Oviedo
Greensboro sit-in (1960) | History, Summary, Impact, & Facts
Finding Safety Data Sheets
Violent Night Showtimes Near Johnstown Movieplex
Bolly2Tolly Maari 2
Horses For Sale In Tn Craigslist
Nottingham Forest News Now
Vht Shortener
Craftybase Coupon
Roseann Marie Messina · 15800 Detroit Ave, Suite D, Lakewood, OH 44107-3748 · Lay Midwife
Cvs Sport Physicals
Rainfall Map Oklahoma
Till The End Of The Moon Ep 13 Eng Sub
Missing 2023 Showtimes Near Grand Theatres - Bismarck
Best Workers Compensation Lawyer Hill & Moin
Toth Boer Goats
Kelley Blue Book Recalls
Chatropolis Call Me
Dogs Craiglist
Embry Riddle Prescott Academic Calendar
Brother Bear Tattoo Ideas
Theater X Orange Heights Florida
Rheumatoid Arthritis Statpearls
10 Best Tips To Implement Successful App Store Optimization in 2024
Sitka Alaska Craigslist
Basic requirements | UC Admissions
When Is The First Cold Front In Florida 2022
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 6001

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.