Tuesday, March 27, 2007

Does link_to_remote() submit using a GET or a POST?

A little while back, I had a post about how to have two submit buttons with form_remote_tag. A question was raised about having a POST as opposed to a GET request. It seemed like a short enough question, but after looking into it for about 15 minutes, I figured it was worth it to type something up.

To recap, when I first was doing web stuff in high school, I never got what the difference between POST and GET were. It seemed like the author of my HTML book then didn't really get it either. However, it wasn't until DHH started raving about REST as a web service did I get it. in short, when a user submits a form, GET puts the form contents in the url, and POST puts the form contents in the body of the HTTP request. The implication (apparently related to REST) is that you'd only use GET for server requests that didn't change content on the server--the request had no side-effects. That way, the same URL would refer to the same resource, time after time. Alternatively, POST is for server requests that change the content of the server. And that's why we care.

I find that reading documentation helps. So I first looked at the browser source for a link_to_remote() call and it's pretty clear it uses Form.serialize() to convert the form, and then sends it along using Ajax.Request(). The question is, does it send via POST or GET?

According to the documentation for link_to_remote(), it sends via POST by default.

The method used is by default POST. You can also specify GET or you can simulate PUT or DELETE over POST. All specified with options[:method]

Example:

  link_to_remote "Destroy", :url => person_url(:id => person), :method => :delete

I also checked it out with a quick check inside a controller with a breakpoint to see if a link_to_remote() call really sends a post. I put a breakpoint inside a controller method called by link_to_remote:
$ ./script/breakpointer 
Executing break point at ./script/../config/../app/controllers/friend_controller.rb:60 in `test_ajax'
irb(#):002:0> request.get?
=> false
irb(#):003:0> request.post?
=> true

As for a regular form_for() call, simply use the :method option. I think by default it also uses POST.

3 comments:

  1. Anonymous12:49 AM

    thanks man, you helped me out!

    ReplyDelete
  2. Anonymous11:38 AM

    Thank you so much for this - I've been puzzling over why my link_to_remote did not work properly in REST. Changed to a POST and perfect!

    ReplyDelete
  3. Thanks. This helped me figure out why my link_to_remote wasn't calling the correct action and was doing a POST instead of a GET like I wanted.

    ReplyDelete