Why are there multiple HTTP Methods available?

后端 未结 1 509
挽巷
挽巷 2021-01-22 13:17

Back when I first started developing client/server apps which needed to make use of HTTP to send data to the server, I was pretty nieve when it came to HTTP methods. I literally

相关标签:
1条回答
  • 2021-01-22 13:30

    I'm going to take a stab at giving a short answer to this.

    GET is used for reading information. It's the 'default' method, and everything uses this to jump from one link to the next. This includes browsers, but also crawlers.

    GET is 'safe'. This means that if you do a GET request, you are guaranteed that you will never change something on the server. If a GET request could cause something to delete on the server, this can be very problematic because a spider/crawler/search engine might assume that following links is safe and automatically delete things.

    This is why we have a couple of different methods. GET is meant to allow you to 'get' things from the server. Likewise, PUT allows you to set something new on a server and DELETE allows you remove something.

    POST's biggest original purpose is submitting forms. You're posting a form to the server and ask the server to do something with that form.

    Any client (a human/browser or machine/crawler) knows that POST is 'unsafe'. It won't do POST requests automatically on your behalf unless it really knows it's what you (the user) wants. It's also used for things like are kinda similar to submitting forms.

    So when you design your website, make sure you use GET only for getting things from the server, and use POST if your ajax request will cause 'something' to change on the server.

    Fun fact: there are a lot of official HTTP methods. At least 30. You'll probably only use a very few of them though.

    So to answer the question in the title more precisely:

    Why are there multiple HTTP Methods available?

    Different HTTP methods have different rules and restrictions. If everyone agrees on those rules, we can start making assumptions about what the intent is. Because these guarantees exists, HTTP servers, clients and proxies can make smart decisions without understanding your specific application.

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