英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论