April 20, 2005

End HTTP abuse

Web access toolkits should make it easier to do the right thing

You'd think that a language with a mere handful of verbs, only two of which are widely spoken, would be easy to learn. Not so in the case of the HTTP, though. After more than a decade, we're still sorting out how and why to use its most common verbs: GET and POST.

This perennial issue recently flared up again when Microsoft's Dare Obasanjo noted that three popular services had got it wrong. Bloglines, Flickr, and del.icio.us all offer APIs that claim to be RESTful -- that is, to conform to the Representational State Transfer principles of HTTP -- yet each violates those principles by allowing GET in some cases where only POST should be allowed.

There are two reasons why a service ought to allow HTTP GET, when it's appropriate to do so. First, the resulting URL remains visible in the browser's address (location) field. That means you can bookmark a request, post it to the Web, or send it to someone in an e-mail. If you've ever wanted to save a search but couldn't, the reason is probably that the form unnecessarily used POST rather than GET.

A second rationale for GET is that various systems along an HTTP pipeline may be able to cache the response.

GET's purpose, however, is only to retrieve information. When you offer a service that updates a resource, you should disallow GET and require clients to use POST. (There are also rarely used HTTP verbs to PUT or DELETE resources, but that's another story.) The reasoning here is twofold: You don't want users to invoke resource-altering requests from hyperlinks. And, such requests aren't cacheable.

Why didn't Bloglines, Flickr, or del.icio.us enforce the POST restriction? I suspect it's because they wanted their APIs to have the broadest possible reach. And from a programmer's perspective, the simplest and easiest HTTP clients are those that use GET.

As an experiment, I sent both GET and POST requests to one of the indicted APIs using four different HTTP client technologies. First up was Curl, the Swiss-army knife of command-line-based URL fetchers. In this case, happily, it was trivial to convert a GET-style request to one that uses POST. I just took the part of the URL that followed the question mark and stuck it after a -d flag. I might have had to look that up, but it's only a small step off the path of least resistance.

The same held true for Python's basic one-line URL fetcher, urllib. In this case I took what followed the URL's question mark and attached it to the optional data argument. Again, just a small extra step.

With Perl, though, I had to take a bigger step. Perl's basic one-line URL fetcher is implemented by the LWP::Simple module. But to switch from GET to POST, I had to construct an LWP::UserAgent object, call its post method, and encode the arguments in a data structure.

Likewise with JavaScript. I could issue a GET request by simply assigning a URL to the location.href object. To issue a POST, though, I had to construct a form or use the nonstandard XMLHttpRequest object.

These aren't huge obstacles. But if all toolkits made POST as accessible as GET -- or as nearly so as possible -- there would be less incentive to misuse GET.

It should be easy to do the right thing.

Close

On Twitter now

Application development

Powered by Twitter

White Paper

D2D Virtual Tape Library Replication Primer

This whitepaper explains the terminology and concepts behind Data Replication technologies and establishes some sizing rules through worked examples. Learn the new paradigm in disaster tolerance—protect data anywhere.

Download now »

White Paper

An Alternative to Virtualization for Datacenter Cost Savings

Server virtualization is a popular option for dealing with mounting datacenter costs. Another equally promising approach is the use of an Application Delivery Controller. Citrix NetScaler provides a low-cost way for organizations to reduce their server count and accrue cost savings from a reduction in space, cooling, power and personnel.

Download now »

White Paper

Why Your Firewall, VPN, and IEEE 802.11i Aren't Enough to Protect Your Network

The emergence of WLANs has created a new breed of security threats to enterprise networks.

Included in HP ProCurve WLAN solutions is security technology that alleviates threats from WLANs through:
* Monitoring wireless activity inside and out of the enterprise
* Classifying WLAN transmissions into harmful and harmless
* Preventing transmissions that pose a security threat to the enterprise network
* Locating participating devices for physical remediation

Download now »

White Paper

Bringing the Edge to the Data Center

Effectively address data protection challenges, implementing solutions that help store and protect business–critical data while cutting costs and improving efficiency and reliability.

Download now »

Sign up to receive InfoWorld Resource Alerts

Subscribe to the Developer World Newsletter

Receive a weekly roundup about the art and science of software development.

©1994-2009 Infoworld, Inc.