When should I use GET function and when should I use POST function?

Asked

Viewed 7,863 times

5

I have a question and I think it’s quite "idiot" but when to use GET and when to use POST ?

I know that when using GET the text or message written in a form or field, it is also passed to the link or URL address, so I know that GET is not used for example, for login or registration.

But what are the features of GET and POST?

  • Related : http://answall.com/questions/16821/por-que-os-browsers-n%C3%A3o-implement-os-protocols-put-e-delete-do-http

  • It’s unrelated, but I appreciate the try

  • @rray if someone who doesn’t have a gold medal votes for one, and one of us votes for the other, they’re both like duplicates. I think by joining your link with what I’ve gone through, it’s complete. There’s only one left without a gold medal "pull the queue".

  • @Bacco cool this other question, the two as reference gets 100%.

  • 1

    And who wants to complete the reasoning, can post answer them, fits more there, then everything is linked.

Show 2 more comments

2 answers

5


You already know how each one works, so I’ll give examples of when to use.

GET passes variables by URL, it is useful for you to share a product page, make pagination, such things... That is, as a benefit you can reproduce a search just by copying the URL, since the variables are in it. Example:

product.php? product=200 (will display product with id 200) You could even save the URL as favorite, would go to the product 200.

With POST you can not do these things I said above, because the data is sent in the body of the HTTP request, as you said, is ideal for login forms, registration, sending files (by get do not give).

In short: the difference is mainly in data visibility.

Note: the GET request is relatively faster, since it is simpler. In the POST request there is a waste of time in the message encapsulation.

  • Basically GET should not be used to change the state of the application, for example, to delete a resource (meusite.com?acao=apagar_noticia&id=1), so it is considered a "safe" method, whereas POST, considered "insecure" method, can and should be used to change the state of the application.

3

The GET has a character limit, varying according to the browser because this limit is based on the size of the URL. Usually using when you want to pass little information. Also, as you yourself observed, with the GET the information is passed through the URL, becoming visible. The function of the GET is basically to retrieve information/resource from the server, in addition, the result of the request can go to the client’s cache (the URL with the information passed on it is saved in the history, for example). It’s faster than the POST, since the information is sent via URL. the GET is limited to sending texts, using ASCII standard

Already the POST is safer, since the URL does not display the information being sent. There is no character limit, and a parallel connection is required to send the information. How a connection is created for sending information, a request HTTP is created for sending. Therefore, in this method, the information sent is in the body of the request, as well as any request HTTP, there are headers that are also sent. It is slower as there is a need to encapsulate the message for sending. Through the POST there is the possibility of sending binary information, in addition to sending texts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.