I have read several sources about HTTP methods, but I still don't understand clearly the simplest thing: what are they for?
Each source I've seen points out when particular methods should be used, but what does it change in practice? Is there any difference how the request is being handled between, let's say, GET and POST?
Or maybe those methods are there to allow us to handle multiple behaviors on one URL?
And finally, what about the browsers? Forms can only make GET and POST requests and they handle them in different way. POST form sends data "in the background", while GET passes them in the URL. Does it have anything to do with the protocol or is it just browsers' convention?
Thank you in advance for clarifying it for me. :)
Fundamentally, yes, methods are there to allow different "interactions" with every "entity". HTTP is designed so you can think of each URL as one entity.
/users
represents all users/users/dave
represents one specific userPOST /users
lets you create a new userPUT /users/dave
lets you modify a specific userGET /users
gets you a list of usersGET /users?name=dave
lets you query for a list of users named "dave"and so on...
That's the way HTTP was designed to be used, each verb has a specific implied meaning. You can use those verbs any way you want really, but GET
implies "passive" information retrieval, while POST
, PUT
and DELETE
imply destructive changes.
Browsers and other clients do handle these differently. It's expected that anything GET
can be requested at any time any number of times, can be cached, can be pre-fetched, can be queried mostly out of order. More destructive actions should be performed only once when requested and not cached, pre-fetched or anything else. A browser will explicitly ask for confirmation if you're "reloading" a page requested via POST
.
POST form sends data "in the background", while GET passes them in the URL. Does it have anything to do with the protocol or is it just browsers' convention?
"In the background" is the wrong way of thinking. The difference is between the URL and the request body. A GET
request should not/must not have anything in its request body. Again, it's only passive information retrieval and must solely consist of HTTP headers. A POST
request can have a request body. A request can have "data" both in its URL and in its body. Again, an assumption is that GET
URLs can be shared and passed around, since it just links to information. POST
requests on the other hand need to be very deliberate, so its information should not and doesn't need to be in the URL.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments