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


当前回答

根据HTTP方法定义操作

HTTP协议定义了许多为请求分配语义的方法。大多数RESTful web api使用的常见HTTP方法有:

GET在指定URI处检索资源的表示形式。响应消息的主体包含所请求资源的详细信息。

POST在指定的URI上创建一个新资源。请求消息的主体提供了新资源的详细信息。注意,POST还可以用于触发不实际创建资源的操作。

PUT在指定的URI上创建或替换资源。请求消息的主体指定要创建或更新的资源。

PATCH执行资源的部分更新。请求体指定要应用于资源的更改集。

DELETE删除指定URI上的资源。

特定请求的效果应取决于资源是集合还是单个项。下表通过电子商务示例总结了大多数RESTful实现采用的常见约定。并不是所有这些请求都可以实现——这取决于具体的场景。

Resource POST GET PUT DELETE
/customers Create a new customer Retrieve all customers Bulk update of customers Remove all customers
/customers/1 Error Retrieve the details for customer 1 Update the details of customer 1 if it exists Remove customer 1
/customers/1/orders Create a new order for customer 1 Retrieve all orders for customer 1 Bulk update of orders for customer 1 Remove all orders for customer 1

POST、PUT和PATCH之间的区别可能令人困惑。

POST请求创建一个资源。服务器为新资源分配URI,并将该URI返回给客户端。在REST模型中,您经常对集合应用POST请求。新资源被添加到集合中。POST请求还可以用于将数据提交到现有资源进行处理,而不需要创建任何新资源。

A PUT request creates a resource or updates an existing resource. The client specifies the URI for the resource. The request body contains a complete representation of the resource. If a resource with this URI already exists, it is replaced. Otherwise, a new resource is created, if the server supports doing so. PUT requests are most frequently applied to resources that are individual items, such as a specific customer, rather than collections. A server might support updates but not creation via PUT. Whether to support creation via PUT depends on whether the client can meaningfully assign a URI to a resource before it exists. If not, then use POST to create resources and PUT or PATCH to update.

PATCH请求对现有资源执行部分更新。客户端为资源指定URI。请求体指定要应用于资源的一组更改。这可能比使用PUT更有效,因为客户端只发送更改,而不是资源的整个表示。从技术上讲,如果服务器支持的话,PATCH还可以创建一个新资源(通过指定一组对“null”资源的更新)。

PUT请求必须是等幂的。如果客户端多次提交相同的PUT请求,结果应该总是相同的(相同的资源将被修改为相同的值)。POST和PATCH请求不能保证是等幂的。

其他回答

根据HTTP方法定义操作

HTTP协议定义了许多为请求分配语义的方法。大多数RESTful web api使用的常见HTTP方法有:

GET在指定URI处检索资源的表示形式。响应消息的主体包含所请求资源的详细信息。

POST在指定的URI上创建一个新资源。请求消息的主体提供了新资源的详细信息。注意,POST还可以用于触发不实际创建资源的操作。

PUT在指定的URI上创建或替换资源。请求消息的主体指定要创建或更新的资源。

PATCH执行资源的部分更新。请求体指定要应用于资源的更改集。

DELETE删除指定URI上的资源。

特定请求的效果应取决于资源是集合还是单个项。下表通过电子商务示例总结了大多数RESTful实现采用的常见约定。并不是所有这些请求都可以实现——这取决于具体的场景。

Resource POST GET PUT DELETE
/customers Create a new customer Retrieve all customers Bulk update of customers Remove all customers
/customers/1 Error Retrieve the details for customer 1 Update the details of customer 1 if it exists Remove customer 1
/customers/1/orders Create a new order for customer 1 Retrieve all orders for customer 1 Bulk update of orders for customer 1 Remove all orders for customer 1

POST、PUT和PATCH之间的区别可能令人困惑。

POST请求创建一个资源。服务器为新资源分配URI,并将该URI返回给客户端。在REST模型中,您经常对集合应用POST请求。新资源被添加到集合中。POST请求还可以用于将数据提交到现有资源进行处理,而不需要创建任何新资源。

A PUT request creates a resource or updates an existing resource. The client specifies the URI for the resource. The request body contains a complete representation of the resource. If a resource with this URI already exists, it is replaced. Otherwise, a new resource is created, if the server supports doing so. PUT requests are most frequently applied to resources that are individual items, such as a specific customer, rather than collections. A server might support updates but not creation via PUT. Whether to support creation via PUT depends on whether the client can meaningfully assign a URI to a resource before it exists. If not, then use POST to create resources and PUT or PATCH to update.

PATCH请求对现有资源执行部分更新。客户端为资源指定URI。请求体指定要应用于资源的一组更改。这可能比使用PUT更有效,因为客户端只发送更改,而不是资源的整个表示。从技术上讲,如果服务器支持的话,PATCH还可以创建一个新资源(通过指定一组对“null”资源的更新)。

PUT请求必须是等幂的。如果客户端多次提交相同的PUT请求,结果应该总是相同的(相同的资源将被修改为相同的值)。POST和PATCH请求不能保证是等幂的。

PUT是一种将内容“上载”到特定URI或覆盖该URI中已有内容的方法。

另一方面,POST是提交与给定URI相关的数据的一种方式。

参考HTTP RFC

据我所知,PUT主要用于更新记录。

POST -创建文档或任何其他资源 PUT—更新创建的文档或任何其他资源。

但要清楚的是,PUT通常“替换”现有的记录,如果它在那里,如果它不在那里创建。

只有语义。

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.