Using cURL for web development
Pre-installed on most OSX and Linux/Unix systems cURL is a great tool for testing and developing a website. Here's a quick overview of what you can do.
cURL provides developers with a useful toolkit to help in developing and debugging websites.
POST, GET, PUT, DELETE ¶
cURL can handle all of these and there are libraries for most of the major programming languages. It handles most common protocols: HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE. You can use it to create functionality or write unit tests.
Viewing response headers ¶
Let’s say you want to check the response headers of a site. Easy using cURL.
curl -I http://www.telegraph.co.uk/
Outputs the HTTP response headers
HTTP/1.1 200 OK
Server: Apache
Last-Modified: Tue, 11 Aug 2009 10:12:19 GMT
ETag: 63-1249985539438
Content-Language: en-GB
Content-Type: text/html;charset=ISO-8859-1
Date: Tue, 11 Aug 2009 10:15:09 GMT
Connection: keep-alive
Profiling responsiveness ¶
cURL comes with a massive array of options. You can find these on the manpage. We can use some of these to profile the responsiveness a site. In this example we look at the Twitter API.
curl -w '\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nPreXfer time:\t%{time_pretransfer}\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' -o /dev/null -s http://twitter.com/statuses/public_timeline.xml
We are using a number of the variables available to show how long it took to resolve the domain name, how long it took to make the connection, the pretransfer time, then how long it took to start the transfer.
The output we get is
Lookup time: 0.024
Connect time: 0.181
PreXfer time: 0.181
StartXfer time: 0.554
Total time: 1.591
Nearly 1.6 seconds - that’s pretty slow.
You can even up your Twitter status using cURL if you want.
curl --basic --user "username:password" --data-ascii "status=dancing round in high heels" http://twitter.com/statuses/update.json
cURL is a great tool for web developers and I’ll certainly be using it more.
References ¶
- cURL Man page
- How to Use cURL to Test RESTful Rails
- PHP: cURL Manual
- Linux Journal | Linux Tips: curl Examples
Tags
Can you help make this article better? You can edit it here and send me a pull request.
See Also
-
Auto update an Ubuntu Server with Aptitude
How to keep software up-to-date automatically on Ubuntu Server using Aptitude -
Writing shell scripts more quickly in vi
A couple of useful tips for writing shell scripts more quickly using the vi editor. -
ExpressionEngine permissions shell script
Every ExpressionEngine install requires that a number of permissions are set on folders and files. Here's a shell script to do the hard work for you.