Curl Cheat Sheet

I have a scribbled sheet on my desk, which is my “cheat sheet” for curl, its really short and I thought I’d put my notes here for safe-keeping. If you’re visiting, then I hope they help you too.

Curl from command line

Curl is a way of making web requests from the command line. I only do this under linux, and if you don’t know why you’d want to do that, then you probably don’t need to read any further (although, that would also make an interesting article!).

curl http://www.lornajane.net

Will give you the output that would be sent to your browser if you requested the main page of this site from there.

Verbose output (-v)

This is where it gets interesting, you can see the actual information being included with the HTTP request when you use the verbose switch.

curl -v http://www.lornajane.net

Try it – you see the headers and the client/server negotiation. Stuff like cache information, browser identification, and cookie information all gets transmitted in these headers.

Using HTTP verbs (-X) and data (-d)

Curl can make requests and include data with them. It can use GET as you’d expect, but POST, PUT and DELETE are all supported too.

curl -X GET "http://www.example.com?page=2&category=toys"

I’ve added the quotes because the ampersand confuses the command line

curl -X POST http://www.example.com/registration.php -d username=lornajane -d password=password -d phone=0123456789

PUT and DELETE can be done in the same way, just using the -X switch.

Further Resources

Actually, if you’ve got this far then you know what you’re doing – use the curl manpage for information on the other switches. Curl can do more things than I think I would ever want to do, not just HTTP, and can spoof cookies and all sorts of other good things. Its especially good for testing web services as it allows clearer diagnosis of the exact behaviour and response. Don’t tell anyone but I have also been known to vardump in the middle of webservices when debugging, and using curl lets you see the output “as it comes” – very handy!

Update: I wrote a follow-up post about Using Cookies with Curl

11 thoughts on “Curl Cheat Sheet

  1. One other useful switch is -H to pass arbitrary request headers, eg -H “X-Foo: bar”. I’m sure there’s a better way, but I usually end up switching things like user-agent (and other things) like this

  2. Kevin: that’s a good addition, thanks for that :)

    Robert: I’m very pleased to hear this was helpful. You can use curl from within PHP itself, I’ve been doing this recently and will write another post on that some time

  3. Having recently written articles about curl and about writing a PHP REST server, I thought I’d complete the circle and put here a few notes on using PHP’s curl wrapper as a Rest client. Also I had to look some of this up when I needed to actually do it s

  4. curl is the C URL library – its a command-line tool for making web requests, with libraries available in many languages. Personally I prefer to use it from the command line and recently I have been using it with cookies for a web services which set a coo

  5. Pingback: Returning JSON using the Accept header in ZF2 – Rob Allen's DevNotes

  6. Pingback: Returning JSON using the Accept header in ZF2 | Rob Allen's DevNotes

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.