Why is the GET method faster than POST in HTTP?

后端 未结 8 2038
暗喜
暗喜 2020-11-30 02:53

I am new to web programming and just curious to know about the GET and POST methods of sending data from one page to another.

It is said that the GET method is faste

相关标签:
8条回答
  • 2020-11-30 03:31

    There are several misconceptions about GET and POST in HTTP. There is one primary difference, GET must be idempotent while POST does not have to be. What this means is that GETs cause no side effects, i.e I can send a GET to a web application as many times as I want to (think hitting Ctrl+R or F5 many times) and the requests will be 'safe'

    I cannot do that with POST, a POST may change data on the server. For example, if I order an item on the web the item should be added with a POST because state is changed on the server, the number of items I've added has increased by 1. If I did this with a POST and hit refresh in the browser the browser warns me, if I do it with a GET the browser will simply send the request.

    On the server GET vs POST is pure convention, i.e. it's up to me as a developer to ensure that I code the POST on the server to not repeat the call. There are various ways of doing this but that's another question.

    To actually answer the question if I use GET or POST to perform the same task there is no performance difference.

    You can read the RFC (http://www.w3.org/Protocols/rfc2616/rfc2616.html) for more details.

    0 讨论(0)
  • 2020-11-30 03:33

    Looking at the http protocol, POST or GET should be equally easy and fast to parse. I would argue, there is no performance difference.

    Take a look at the raw HTTP headers

    http GET

    GET /index.html?userid=joe&password=guessme HTTP/1.1
    Host: www.mysite.com
    User-Agent: Mozilla/4.0
    

    http POST

    POST /login.jsp HTTP/1.1
    Host: www.mysite.com
    User-Agent: Mozilla/4.0
    Content-Length: 27
    Content-Type: application/x-www-form-urlencoded
    
    userid=joe&password=guessme
    

    From my point of view, performance should not be considered when comparing GET and POST.

    0 讨论(0)
  • 2020-11-30 03:34

    You should think of GET as "a place to go", and POST as "doing something". For example, a search form should be submitted using GET because the search result page is a "place" and the user will want to bookmark it or retrieve it from their history at a later date. If you submit the form using POST the user can only recreate the page by submitting the form again. On the other hand, if you were to perform an action such as clicking a delete button, you would not want to submit this with GET, as the action would be repeated whenever the user returned to the URL.

    0 讨论(0)
  • 2020-11-30 03:37

    I agree with other answers, but it was not mentioned that GET requests can be cached while POST requests are never cached. I think this is the main reason for some GET request being performed faster. (Of-coarse this means that sometimes no request is actually sent. Hence it's not actually the GET request which is faster, but your browser's cache.)

    HTTP Methods: GET vs. POST: http://www.w3schools.com/tags/ref_httpmethods.asp

    0 讨论(0)
  • 2020-11-30 03:40

    It's not much about speed. There are plenty of cases where POST is more applicable. For example, search engines will index GET URLs and browsers can bookmark them and make them show up in history. As a result, if you take actions like modifying a DB based on a GET request, it might be harmful as some bots might also traverse the URL.

    The other case can be security issue. If you post credentials using GET, it'll get listed in browser history and server log files.

    0 讨论(0)
  • 2020-11-30 03:42

    POST will grow your headers more, just making it larger, but the difference ought to be negligible really, so I don't see why this should be a concern.

    Just bear in mind that the proper way to speak HTTP is to use GET only for actions and POST for data. You don't have to, but you also don't want to have a case where Google bots can, for example, insert, delete or manipulate data that was only meant for a human to handle simply because it is following the links it finds.

    0 讨论(0)
提交回复
热议问题