为什么有多种 HTTP 方法可用? [英] Why are there multiple HTTP Methods available?

查看:39
本文介绍了为什么有多种 HTTP 方法可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我第一次开始开发需要使用 HTTP 将数据发送到服务器的客户端/服务器应用程序时,我对 HTTP 方法非常陌生.我真的对一切都使用了 GET 请求.

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 used GET requests for EVERYTHING.

我后来了解到我应该使用 POST 发送数据并使用 GET 请求数据,但是,我有点困惑为什么这是最佳实践.从功能的角度来看,我能够使用 GET 或 POST 来实现完全相同的功能.

I later learned that I should use POST for sending data and GET for requesting data however, I was slightly confused as to why this is best practice. From a functionality perspective, I was able to use either GET or POST to achieve the exact same thing.

为什么使用特定的 HTTP 方法而不是对所有事情都使用相同的方法很重要?

Why is it important to use specific HTTP methods rather than using the same method for everything?

我知道 POST 比 GET 更安全(GET 使数据在 HTTP URL 中可见)但是,我们不能对所有内容都使用 POST 吗?

I understand that POST is more secure than GET (GET makes the data visible in the HTTP URL) however, couldn't we just use POST for everything then?

推荐答案

我将尝试简要回答这个问题.

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

GET 用于读取信息.这是默认"方法,一切都使用它从一个链接跳转到下一个链接.这包括浏览器,也包括爬虫.

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 是安全的".这意味着,如果您执行 GET 请求,则可以保证您永远不会更改服务器上的某些内容.如果 GET 请求可能导致服务器上的某些内容被删除,这可能会带来很大问题,因为蜘蛛/爬虫/搜索引擎可能会认为以下链接是安全的并自动删除内容.

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.

这就是为什么我们有几种不同的方法.GET 旨在允许您从服务器获取"东西.同样,PUT 允许你在服务器上设置一些新的东西,而 DELETE 允许你删除一些东西.

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 最大的原始目的是提交表单.您正在向服务器发布一个表单,并要求服务器对该表单进行处理.

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.

任何客户端(人/浏览器或机器/爬虫)都知道 POST 是不安全的".它不会代表您自动执行 POST 请求,除非它真的知道这是您(用户)想要的.它也用于类似于提交表单的事情.

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.

所以当你设计你的网站时,确保你只使用 GET 来从服务器获取东西,如果你的 ajax 请求会导致 'something' 使用 POST在服务器上更改.

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.

有趣的事实:有很多官方HTTP 方法.至少 30 个.不过,您可能只会使用其中的极少数.

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:

为什么有多种 HTTP 方法可用?

Why are there multiple HTTP Methods available?

不同的 HTTP 方法有不同的规则和限制.如果每个人都同意这些规则,我们就可以开始假设意图是什么.由于存在这些保证,HTTP 服务器、客户端和代理可以在不了解您的特定应用程序的情况下做出明智的决定.

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.

这篇关于为什么有多种 HTTP 方法可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆