GET 还是 POST?动作不破坏,参数很多,不用缓存
GET or POST? Action not destructive, a lot of parameters, no need to cache
我正在设计一个 API 端点,它 运行 是一个模拟,returns 结果。
- 具体的模拟是运行取决于很多参数。
- 没有副作用。没有破坏性的(创建、更新、删除)发生。
- 我不想缓存URL查询字符串中的参数供用户保存,或者点击刷新。
此端点应该接受 GET 请求还是 POST 请求?
- 查询字符串不够大,无法容纳所有参数。 apparently 您不应该随 GET 请求一起发送负载。
- 没有破坏性的副作用(完全没有副作用)。所以POSTdoesn't seem也合适。
我该怎么办?
我相信对于这样的事情你会希望有一个POST
请求和return用户在JSON
或[=13=中的Response
中的东西].我正在为一个发送 POST
的站点使用 API
,我从 Response
中获取数据,并且我要传递很多参数。
首先,应该重构和简化这个 API 端点。应避免带有大量参数的 HTTP 请求,无论是 GET 还是 POST 或其他。带有大量参数的请求意味着两个模块之间的耦合非常紧密——客户端必须非常小心地 assemble 以满足服务器的要求。此外,带有大量参数的请求会带来文档和培训成本。
也就是说,必须是POST,如果很多参数无法避免的话。原因是:虽然应该是GET(语义上),但在这种情况下GET是不可行的——所有参数的请求都超过了查询字符串的最大限制。浏览器可能会截断查询字符串并中断请求。
综上所述,不是我应该做什么的问题,而是我必须做什么的问题,除非优化了 API 端点。
Should this endpoint accept GET requests, or POST requests?
我有一些好消息要告诉你。 REST 不关心。
谜语:您将如何在网站上提供这项服务?
您可能有一些普遍感兴趣的登录页面,然后在该页面上添加 link“单击此处试用模拟器!”当消费者遵循该 link 时,您将提供描述模拟所需参数的表单表示,以及端点 和操作 的标识符。消费者将提交填写好的表格,将模拟器参数的表示发送到您的端点。
超媒体 API 的工作方式相同;客户端不需要知道端点或使用什么方法。它需要知道的是如何从表单的表示中获取该信息。
如果您有超媒体 API,您可以更改端点,或在 http 方法之间来回切换,而无需更新客户端以匹配。
There are no destructive side effects (no side effects at all). So POST doesn't seem appropriate either.
我有更多好消息要告诉你。使用 POST 是 很好 。当前使用 POST 的权限不是堆栈溢出,而是 RFC-7231
The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics. For example, POST is used for the following functions (among others):
- Providing a block of data, such as the fields entered into an HTML form, to a data-handling process
完美。它不可缓存,除非您明确地这样做。它包含用于将用户重定向到数据的可缓存表示的工具,以用于有意义的情况。
POST不会做的是与浏览器或中间组件进行通信,确保重试丢失的消息是安全的。
这是菲尔丁在 2009
中对此所说的话
It isn’t RESTful to use POST for information retrieval when that information corresponds to a potential resource, because that usage prevents safe reusability and the network-effect of having a URI.
POST only becomes an issue when it is used in a situation for which some other method is ideally suited: e.g., retrieval of information that should be a representation of some resource (GET), complete replacement of a representation (PUT), or any of the other standardized methods that tell intermediaries something more valuable than “this may change something.” The other methods are more valuable to intermediaries because they say something about how failures can be automatically handled and how intermediate caches can optimize their behavior. POST does not have those characteristics, but that doesn’t mean we can live without it. POST serves many useful purposes in HTTP, including the general purpose of “this action isn’t worth standardizing.”
HTTP 没有为包含负载的 safe 操作指定方法。它
确实指定了 idempotent method that includes a payload; PUT。使用 PUT 是 不寻常的 ,因为它并不真正符合对“创建”或“更新”的通常理解,但只要您注意标识符,我认为有效.
PUT does not mean store. I must have repeated that a million times in webdav and related lists. HTTP defines the intended semantics of the communication -- the expectations of each party. The protocol does not define how either side fulfills those expectations, and it makes damn sure it doesn't prevent a server from having absolute authority over its own resources.
我的理解是:
- 服务器不受限于按原样跟踪资源状态;它可以使用输出表示,而不是输入表示
- 服务器对允许的访问没有限制;资源可以是只写的。或者获取资源可以再次提供其输入表示。
- 服务器不限制更改的持久性; “我们成功处理了您的请求(但随后立即恢复了结果)”完全有效。
来自 RFC 7231:
A successful response only implies that the user agent's intent was achieved at the time of its processing by the origin server.
此外,200 status代码的定义给了你一些空间
For the methods defined by this specification, the intended meaning of the payload can be summarized as:
- GET a representation of the target resource
- PUT, DELETE a representation of the status of the action;
所以我认为,经过详细审查,这个选项可能比 POST 或 GET 更适合您的特定情况。
我正在设计一个 API 端点,它 运行 是一个模拟,returns 结果。
- 具体的模拟是运行取决于很多参数。
- 没有副作用。没有破坏性的(创建、更新、删除)发生。
- 我不想缓存URL查询字符串中的参数供用户保存,或者点击刷新。
此端点应该接受 GET 请求还是 POST 请求?
- 查询字符串不够大,无法容纳所有参数。 apparently 您不应该随 GET 请求一起发送负载。
- 没有破坏性的副作用(完全没有副作用)。所以POSTdoesn't seem也合适。
我该怎么办?
我相信对于这样的事情你会希望有一个POST
请求和return用户在JSON
或[=13=中的Response
中的东西].我正在为一个发送 POST
的站点使用 API
,我从 Response
中获取数据,并且我要传递很多参数。
首先,应该重构和简化这个 API 端点。应避免带有大量参数的 HTTP 请求,无论是 GET 还是 POST 或其他。带有大量参数的请求意味着两个模块之间的耦合非常紧密——客户端必须非常小心地 assemble 以满足服务器的要求。此外,带有大量参数的请求会带来文档和培训成本。
也就是说,必须是POST,如果很多参数无法避免的话。原因是:虽然应该是GET(语义上),但在这种情况下GET是不可行的——所有参数的请求都超过了查询字符串的最大限制。浏览器可能会截断查询字符串并中断请求。
综上所述,不是我应该做什么的问题,而是我必须做什么的问题,除非优化了 API 端点。
Should this endpoint accept GET requests, or POST requests?
我有一些好消息要告诉你。 REST 不关心。
谜语:您将如何在网站上提供这项服务?
您可能有一些普遍感兴趣的登录页面,然后在该页面上添加 link“单击此处试用模拟器!”当消费者遵循该 link 时,您将提供描述模拟所需参数的表单表示,以及端点 和操作 的标识符。消费者将提交填写好的表格,将模拟器参数的表示发送到您的端点。
超媒体 API 的工作方式相同;客户端不需要知道端点或使用什么方法。它需要知道的是如何从表单的表示中获取该信息。
如果您有超媒体 API,您可以更改端点,或在 http 方法之间来回切换,而无需更新客户端以匹配。
There are no destructive side effects (no side effects at all). So POST doesn't seem appropriate either.
我有更多好消息要告诉你。使用 POST 是 很好 。当前使用 POST 的权限不是堆栈溢出,而是 RFC-7231
The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics. For example, POST is used for the following functions (among others):
- Providing a block of data, such as the fields entered into an HTML form, to a data-handling process
完美。它不可缓存,除非您明确地这样做。它包含用于将用户重定向到数据的可缓存表示的工具,以用于有意义的情况。
POST不会做的是与浏览器或中间组件进行通信,确保重试丢失的消息是安全的。
这是菲尔丁在 2009
中对此所说的话It isn’t RESTful to use POST for information retrieval when that information corresponds to a potential resource, because that usage prevents safe reusability and the network-effect of having a URI.
POST only becomes an issue when it is used in a situation for which some other method is ideally suited: e.g., retrieval of information that should be a representation of some resource (GET), complete replacement of a representation (PUT), or any of the other standardized methods that tell intermediaries something more valuable than “this may change something.” The other methods are more valuable to intermediaries because they say something about how failures can be automatically handled and how intermediate caches can optimize their behavior. POST does not have those characteristics, but that doesn’t mean we can live without it. POST serves many useful purposes in HTTP, including the general purpose of “this action isn’t worth standardizing.”
HTTP 没有为包含负载的 safe 操作指定方法。它 确实指定了 idempotent method that includes a payload; PUT。使用 PUT 是 不寻常的 ,因为它并不真正符合对“创建”或“更新”的通常理解,但只要您注意标识符,我认为有效.
PUT does not mean store. I must have repeated that a million times in webdav and related lists. HTTP defines the intended semantics of the communication -- the expectations of each party. The protocol does not define how either side fulfills those expectations, and it makes damn sure it doesn't prevent a server from having absolute authority over its own resources.
我的理解是:
- 服务器不受限于按原样跟踪资源状态;它可以使用输出表示,而不是输入表示
- 服务器对允许的访问没有限制;资源可以是只写的。或者获取资源可以再次提供其输入表示。
- 服务器不限制更改的持久性; “我们成功处理了您的请求(但随后立即恢复了结果)”完全有效。
来自 RFC 7231:
A successful response only implies that the user agent's intent was achieved at the time of its processing by the origin server.
此外,200 status代码的定义给了你一些空间
For the methods defined by this specification, the intended meaning of the payload can be summarized as:
- GET a representation of the target resource
- PUT, DELETE a representation of the status of the action;
所以我认为,经过详细审查,这个选项可能比 POST 或 GET 更适合您的特定情况。