GET request can be bookmarked and POST can not . Can anybody explain on this?
I am studying the HTTP methods. I read that GET request can be bookmarked and POST request can not be bookmarked. Can anybody explain this with an 开发者_JAVA百科example?
Thanks
An HTTP POST can be bookmarked, but since a bookmark only consists of the URL, all of the form parameters will be lost. This will often mean that the web server doesn't know what to do with the request, since it was expecting some form parameters.
If you submit a form via a GET request, all of the form parameters go into the URL (after the ?), so a bookmark will contain all of the information needed for the webserver to rebuild the page a second time (except for cookies, perhaps, but a webserver is more likely to handle that gracefully)
A POST cannot be bookmarked. Attempting to bookmark a POST will just result in a GET operation on the URL. There's a very good reason for this, GET requests are supposed to be idempotent - that is, making the same GET request numerous times should result in the same response. POST requests on the other hand are not. Allowing POSTs to be bookmarked could result in you paying for something twice, transferring money out of your bank account again, etc.
It is not that the one or other cannot be bookmarked. It is more that clicking a bookmark will always fire a GET
request! Clicking a plain vanilla link, bookmark, browser nav button, entering browser address bar, etcetera all fires a GET
request. They never fires a POST
request. A POST
request is normally only fired when you submit a HTML <form>
which is set with method="post"
. You can perfectly program software to bookmark a POST
request, but it is not done so due to sensitive reasons.
The special thing here however is that a GET
request is idempotent. In other words, it is guaranteed to give the same result everytime when you invoke it. That is quite simple as all request parameters are just available in the request URL. You can use it to preprocess data prior to display. In Java Servlet world, you normally use doGet()
method for this which preloads some stuff based on the request parameters at end forwards the request to a JSP file for display.
A POST
request however is not idempotent. In other words, it is not guaranteed to give the same result everytime when you invoke it. All request parameters are included in the request body. It is not visible for the enduser and also not visible in the request URL. In other words, you cannot bookmark it. You can use it ro postprocess data after a form submit. In Java Servlet world, you normally use doPost()
method for this which gathers the request parameters and stores it in some database and at end forwards or redirects the request to a JSP file for result/confirmation/display.
In a GET
request all the information for the request is encoded in the URL. In a POST
request the request is made to the specified URL, but all the information related to the request, e.g. form content, is passed in the request body.
When you bookmark a URL you're only bookmarking the URL, not the request body. E.g. if you fill in a form on webpage that is then passed to the webserver as a POST
request, and you then bookmark the response page in your browser, the browser will make note of only the URL, but not the form body.
Also, when you bookmark a page in a browser, the bookmark includes only the URL, but not the request method. The browser always uses GET
for all bookmarks.
In fact that's not requests are bookmarked but URIs.
GET parameters are sent as part of URL whereas POST parameters are sent in HTTP request body. POST is like GET with second hidden set of arguments that aren't visible to user. This is why you are able to make POST request on URL that contains GET parameters.
For details see How are parameters sent in an HTTP POST request?
Though normally bookmarks contain URLs, they may also contain JavaScript code (so named bookmarklets) and there are hacks to make bookmarks that would make POST request instead of GET.
For details see How to save a bookmark in Firefox with POST data?
精彩评论