你应该发送GET还是POST,如果字段的计算基于查询参数?

huangapple go评论82阅读模式
英文:

What should i send GET or POST if field calculating based on query params?

问题

I have resource /api/v1/serviced-objects/ and when I send a regular GET request, I get a server response, and each element in it contains a distance field with a null value.

Also, I have a query parameter user_point. If I send a GET request like /api/v1/serviced-objects/?user_point=0,0, I'll get a calculated value for the distance field between the given user_point and the serviced object point in meters.

So, the general question is - Is it still idempotent, or is it better to use POST in this case?

英文:

I have resource /api/v1/serviced-objects/ and when i send regular GET request, i get server response and each element in it contains distance field which has null value.

{
  "count": 2,
  "next": null,
  "previous": null,
  "results": [
    {
      "id": 1,
      "point": "0,30",
      "distance": null
    },
    {
      "id": 2,
      "point": "30,0",
      "distance": null
    }
  ]
}

Also i have query param user_point. If i send GET request like /api/v1/serviced-objects/?user_point=0,0, i'll get calculated value for distance field between given user_point and serviced object point in meters.

{
  "count": 2,
  "next": null,
  "previous": null,
  "results": [
    {
      "id": 1,
      "point": "0,30",
      "distance": 2000
    },
    {
      "id": 2,
      "point": "30,0",
      "distance": 1000
    }
  ]
}

So general question is - Is it still idempotent or it's better to use POST in this case?

答案1

得分: 1

你可以根据你对在应用程序上下文中计算和更新distance字段的过程的看法来选择使用带有查询参数的GET请求或POST请求。如果操作主要涉及检索资源,则可以使用GET请求。另一方面,如果涉及创建或修改资源,则POST请求可能更合适。

在给定示例中,包括user_point参数和distance字段的计算不会影响GET请求的幂等性,因为更有可能没有修改服务器上的数据,只是基于提供的参数检索信息。虽然响应可能会根据user_point参数而异,但具有相同user_point值的请求应始终产生相同的响应。

英文:

How you choose between using a GET request with query parameters or a POST request relies on how you think about the process of calculating and updating the distance field in your application's context. Using the GET request is acceptable if the action primarily involves retrieving resources. A POST request on the other hand, would be more appropriate if it's about creating or modifying a resource.

In the given example, the inclusion of the user_point parameter and distance field calculation does not affect the idempotency of the GET request because more probably no data is being modified on the server and it is simply retrieving information based on the provided parameter. While the response may differ based on the user_point parameter, requests with the same user_point value should always produce the same response.

答案2

得分: 0

短答案是,在您的系统中没有修改任何资源值的情况下,应选择GET请求,否则选择POST

英文:

The short answer is till the time you are not modifying any resource value in your system you should go for the GET request, otherwise POST

huangapple
  • 本文由 发表于 2023年6月13日 12:02:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定