4 Useful cURL Tricks

cURL is pretty much the universal go-to tool for testing HTTP responses from the command line. If you’re not already familiar with it, here are a few handy things you can use it for.

Checking Headers

You can use cURL to see the headers sent in a request. I use this all the time to see if the headers I’m trying to send in a script are working right.

$ curl -I http://www.google.com
HTTP/1.1 200 OK
Date: Thu, 06 Dec 2012 02:45:58 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
(...)

Sending POST Requests

I was working on a project recently that involved receiving POST requests from a client script running on another server. Since there was no convenient HTML form to use to repeatedly test changes made to the API, I ended up using cURL. Sending POST data is a simple matter of passing an argument that looks something like a query string.

curl http://example.org/url-to-recieve-post-request -d "name=Gandalf&type=wizard";


Spoofing User Agents

You can edit the user agent sent along with the request, which is another thing I’ve found useful in testing.

curl http://example.org -d "name=Gandalf&type=wizard" --user-agent "WordPress"

Downloading Files

While my first instinct is usually to use wget to quickly snag a remote file, that particular package isn’t installed on every system. I don’t believe OS X includes it by default (though that’s nothing Homebrew can’t take care of), for example. cURL can also be used to quickly download and save a remote URL to your current directory.

curl -O http://wordpress.org/latest.zip