For example, I do not understand the concept that PUT should be used to update and not POST, when sending a form from html with its values, once these values are received in the backend, what is done is to write the code in PHP for example , to communicate with the database and make the query to Update a specific record, regardless of whether the method was PUT, POST or GET.
What is really the difference between using one or the other?
According to ilpaijin 's answer in English SO these are the differences, to know more about it I recommend you read his answer , it is well explained and with clear examples.
The protocol dictates the following:
I put some links to MDN web Docs where they explain and give examples of each one of them and others in case you want to read about it and get better information.
I hope it works for you.
HTTP is a communication protocol for sending hypertext files with links and so on. And like any protocol, it has some rules, and its commands have a specific semantics.
The command
GET
is used to request a resource from a machine, such as an HTML web page, images, etc. The only response from the machine (server) should be the resource if it has one, or an error message. If it does, it will reply with a message200 OK
with the resource you requested embedded in that message.If what you want is to send information to the server, for example through a form, it is normal to use the method
POST
, which sends data for the server to process. The response from the server should only be an indication of the status of such processing.On the other hand, the method
PUT
is focused on updating a server resource, also having other differences, such as the supplied URI.More information about the different methods and uses of the HTTP protocol in Wikipedia , and in RFC 1945 , as well as in subsequent revisions of it.
You also have information here about HTTP response codes, which is what the server machine responds to a request.
Edition:
While you can use a
POST
to request a resource, it is not okay to do so. First, it breaks the semantics of the protocol. And secondly, the 3 methods you mention are not interchangeable in all situations.The clearest difference between a
GET
and aPOST
is how any extra information is communicated to the server. A requestPOST
has a body in which the information that you want to send is added, such as the fields of a form filled in by a user.In a request
GET
, on the other hand, the information that is sent to the server (which should be things like search filters, etc.) is put directly into the URL of the request.So a request
GET
would look like this:GET http://www.google.es/search?q=http+get HTTP/1.1
Plus some headers with meta information (language, user-agent, mime-type, etc).
Where what is to the right of the question mark is the extra information that is supplied to the server when requesting a resource, if there is more than one argument they are separated by & symbols.
On the other hand, a request
POST
would indicate the extra information, often obtained through forms, in the body of the message, not in the URL, remaining as:POST http://www.google.es/search HTTP/1.1
The latter, of course, is just an example.
In addition, the method
POST
allows you to send data in different ways: as plain text, as a text string in the styleGET
but in the body, or as block data (multipart), which gives you more flexibility when sending a lot of data to the server (as when uploading a code file to GitHub, for example, imagine having to URL-encode an entire file).For those interested, I have found more information about this, and it is that when we talk about HTML forms , they only support the GET and POST methods , if we decide to do something like for example
<form action="localhost/prueba.html" method="HEAD"></form>
or with PUT, or any other that is not GET / POST, the browser will automatically convert those methods to GET. On the contrary, if we want to use if or if these methods are implemented in JS, that is, using AJAX , we should be able to make requests with ALL THE METHODS. For more information: https://www.it-swarm.dev/es/html/usando-el-metodo-put-en-forma-html/941772569/ I hope it helps! Cheers