Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET or POST, which method to use for submitting a form?

I'm writing a web form for my Ruby on Rails app. The form has a text field, some checkboxes, a set of radio buttons and two text boxes.

What are the pluses and minuses of using GET over POST and vice versa. I always thought you should use GET for retrieving the form and POST for submitting, but I've just learnt you can do both. Does it really make a difference? Cheers.

<% form_tag({ :action => "create" }, :method => "get") do %>
like image 901
alamodey Avatar asked Nov 29 '22 05:11

alamodey


2 Answers

GET requests are always added to the URL, where as POST is submitted with the body of the request. As you note both can be used to retrieve and send data, but there are some distinctions:

  1. As GET is sent with the URL you are limited in size to the maximum length of the query string. This varies from browser to browser, but is usually at least around 2000 characters (on modern browsers). This usually makes it inappropriate for sending large text fields (eg email).

  2. AS the GET command is exposed in the query string it can be easily modified by the user

  3. As the GET command is in the query string it does make it easier for users to bookmark a specific page, assuming your page will work with some state variables stored.

  4. POST is usually more appropriate for sending data, as it is suits the nature of a request, mostly because of the limitations of the above.

like image 52
Chris Avatar answered Dec 04 '22 09:12

Chris


The HTML specifications technically define the difference between both as "GET" means that form data is to be encoded (by a browser) into a URL while the "POST" means that the form data is to appear within a message body.

But the usage recommendation would be that the "GET" method should be used when the form processing is "idempotent", and in those cases only. As a simplification, we might say that "GET" is basically for just getting (retrieving) data whereas "POST" may involve anything, like storing or updating data, or ordering a product, or sending E-mail.

like image 24
simplyharsh Avatar answered Dec 04 '22 11:12

simplyharsh