它们似乎都在向身体内部的服务器发送数据,那么它们有什么不同呢?


当前回答

Post和Put主要用于发布数据和其他更新数据。但是你可以只对post request做同样的事情。

其他回答

POST被认为是某种工厂类型的方法。您将数据包含在其中以创建您想要的内容,而另一端的任何内容都知道如何使用它。PUT用于更新给定URL上的现有数据,或者当您知道URI将是什么并且它还不存在时创建一些新的东西(与POST相反,POST将创建一些东西并在必要时返回URL)。

简单来说,你可以说:

1.HTTP Get:用于获取一个或多个条目

2.HTTP Post:用于创建条目

3.HTTP Put:用于更新条目

4.HTTP补丁:用于部分更新项目

5.HTTP删除:用于删除项目

给出rest风格资源的示例:

带有一堆图书信息的POST /books可能会创建一本新书,并响应标识该图书的新URL: /books/5。

PUT /books/5必须创建一个ID为5的新书,或者用ID 5替换现有的书。

在非资源风格中,POST可以用于几乎任何具有副作用的事情。另一个区别是PUT应该是幂等的:同一个数据的多个PUT到同一个URL应该没问题,而多个POST可能会创建多个对象或POST操作所做的任何事情。

只有语义。

HTTP PUT应该接受请求体,然后将其存储在由URI标识的资源中。

HTTP POST更为通用。它应该在服务器上发起一个操作。该操作可以是将请求体存储在由URI标识的资源中,也可以是不同的URI,也可以是不同的操作。

PUT类似于文件上传。对URI的put操作恰好影响该URI。对URI的POST可以产生任何效果。

GET: Retrieves data from the server. Should have no other effect. PUT: Replaces target resource with the request payload. Can be used to update or create a new resource. PATCH: Similar to PUT, but used to update only certain fields within an existing resource. POST: Performs resource-specific processing on the payload. Can be used for different actions including creating a new resource, uploading a file, or submitting a web form. DELETE: Removes data from the server. TRACE: Provides a way to test what the server receives. It simply returns what was sent. OPTIONS: Allows a client to get information about the request methods supported by a service. The relevant response header is Allow with supported methods. Also used in CORS as preflight request to inform the server about actual the request method and ask about custom headers. HEAD: Returns only the response headers. CONNECT: Used by the browser when it knows it talks to a proxy and the final URI begins with https://. The intent of CONNECT is to allow end-to-end encrypted TLS sessions, so the data is unreadable to a proxy.