Tuesday, March 13, 2007

I finally GET POST

2007-03-13-PostDialogWarning.jpg
The little popup box in browsers saying "The page you are trying to view contains POSTDATA..." has always annoyed me. I just want to refresh the page, not be interrupted by a confirm dialog box. I even checked for firefox extensions to skip the dialog and just do the repost like the more friendly GETs. Now I realize how wrong I was. The dialog box annoyed me because it's my friend, and friends tell you things you don't want to hear to help you.
I used to think POSTs were used when the data being sent back to the server was bigger than could fit on a URL string, or you didn't want to spend the time url encoding a bunch of data. I now see how naive that was.
I came to this enlightenment late in my career through a little web site I'm building to learn Rails called CleanJokesForKids.com
2007-03-13-Jokes.jpg
Kids submit their jokes and vote on other jokes by clicking an href link on the smiley/frowny faces. I was overjoyed in the first week with the terrific number of voters. Hundreds of votes were being cast. I ignored the fact that the average for all the jokes was something like 2.984 or 3.023. Then the sad truth dawned on me. Search engines were scanning the page and following each link and voting five times for each joke. This made the vote count high and the results close to the average of 3.0.
I needed a way to stop search engines from voting - that way is POST.
POST is not used for large data streams, but operations that are not idempotent. Idempotent basically means operations that can be repeated without altering the underlying data. If you refresh the front page of amazon.com, no data is changed on the back end - it's idempotent. If you are buying a book and refresh, you can buy two books - that is not idempotent.
Simple hrefs and GETs are cool. GETs are fun. Links with GET allow the user to bookmark pages, search engines to index the page, and users to refresh easily. Only use POSTs when your data is not idempotent. Search engines know not to follow POSTs.
BTW, make the world a better place and share your favorite kid joke at CleanJokesForKids.com.

No comments: