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

查看:121
本文介绍了为什么有多个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请求将导致服务器上的某些内容"发生更改,则请使用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天全站免登陆