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

3

I have a question and I think it's even "stupid" but when should I use GET and when should I 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?

    
asked by anonymous 15.03.2016 / 14:22

2 answers

5

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

GET passes variables by URL, it's useful for you to share a product page, do paging, things like that ... 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 the product with id 200) You could even save the URL as a favorite, would go for the 200 product.

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

In short: the difference lies mainly in the visibility of the data.

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

    
15.03.2016 / 15:22
3

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 on little information. Also, as you yourself noted, with GET the information is passed through the URL, making it visible. The function of GET is basically to retrieve an 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 in it is saved in the history, for example). It is faster than POST , since information is sent via URL. GET is limited to sending texts using ASCII standard

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. Because a connection is created to send information, a HTTP request is created for sending. Because of this, in this method, the information sent is in the body of the request, and like every request HTTP , there are headers that are also sent. It is slower because there is a need to encapsulate the message for sending. Through POST there is the possibility of sending binary information, in addition to sending texts.

    
15.03.2016 / 15:05