如果查询字符串用于查找需要更新的资源,那么可以使用带有查询字符串的 HTTP POST 方法吗?
Is it okay to use HTTP POST method with a query string, if a query string is used to find the resources that need to be updated?
我的 HTTP API 有一个更新过滤器找到的资源的方法。实际上,它是一种 POST 方法,它利用查询字符串 来查找 所需的资源,并在其主体中传输数据 以更新 找到的资源:
POST /module/resources?field_1=abc&field_2=def&field_n=xyz
body: {
"desired_field":"desired_data"
}
我听说将 POST 方法和查询字符串一起使用可能会产生代码异味,但在上述情况下,我认为这完全合理。
我是不是做出了错误的假设?
在谈论资源时,从 HTTP/REST 的角度来看没有区别:
/article/1
/article?id=1
因此,如果您对其中任何一个执行 GET
请求以获取文章,您可以对其中任何一个执行 PUT
或 PATCH
以进行更改。
但是,许多开发人员想到查询参数和 POST 主体的方式通常是 'just a different way to send parameters'。这是不正确的,因为它们具有截然不同的含义,但同时使用两者可能会使某些人感到困惑。
因此,在协议级别上,您所做的一切都很好。我认为使用 URI 作为定位器并使用正文作为主要消息是一种优雅的做法。
Is it okay to use HTTP POST method with a query string, if a query string is used to find the resources that need to be updated?
是的。
Am I making wrong assumptions here?
没有
关键的想法是,作为资源的作者,您可以选择其标识符
REST relies instead on the author choosing a resource identifier that best fits the nature of the concept being identified. -- Fielding, 2000
查询是资源标识符的一部分
The query component contains non-hierarchical data that, along with data in the path component (Section 3.3), serves to identify a resource within the scope of the URI's scheme and naming authority -- RFC 3986
并且 HTTP 方法的语义在所有 资源
中标准化
Once defined, a standardized method ought to have the same semantics when applied to any resource, though each resource determines for itself whether those semantics are implemented or allowed. -- RFC 7231
我的 HTTP API 有一个更新过滤器找到的资源的方法。实际上,它是一种 POST 方法,它利用查询字符串 来查找 所需的资源,并在其主体中传输数据 以更新 找到的资源:
POST /module/resources?field_1=abc&field_2=def&field_n=xyz
body: {
"desired_field":"desired_data"
}
我听说将 POST 方法和查询字符串一起使用可能会产生代码异味,但在上述情况下,我认为这完全合理。
我是不是做出了错误的假设?
在谈论资源时,从 HTTP/REST 的角度来看没有区别:
/article/1
/article?id=1
因此,如果您对其中任何一个执行 GET
请求以获取文章,您可以对其中任何一个执行 PUT
或 PATCH
以进行更改。
但是,许多开发人员想到查询参数和 POST 主体的方式通常是 'just a different way to send parameters'。这是不正确的,因为它们具有截然不同的含义,但同时使用两者可能会使某些人感到困惑。
因此,在协议级别上,您所做的一切都很好。我认为使用 URI 作为定位器并使用正文作为主要消息是一种优雅的做法。
Is it okay to use HTTP POST method with a query string, if a query string is used to find the resources that need to be updated?
是的。
Am I making wrong assumptions here?
没有
关键的想法是,作为资源的作者,您可以选择其标识符
REST relies instead on the author choosing a resource identifier that best fits the nature of the concept being identified. -- Fielding, 2000
查询是资源标识符的一部分
The query component contains non-hierarchical data that, along with data in the path component (Section 3.3), serves to identify a resource within the scope of the URI's scheme and naming authority -- RFC 3986
并且 HTTP 方法的语义在所有 资源
中标准化Once defined, a standardized method ought to have the same semantics when applied to any resource, though each resource determines for itself whether those semantics are implemented or allowed. -- RFC 7231