简体   繁体   English

更新现有资源时,API REST返回新资源

[英]API REST returning new resource when updating existing one

I have a ProjectStep resource in my application and I have to create an API endpoint that will be used to update my ProjectStep to mark it as finished and create the next ProjectStep. 我的应用程序中有一个ProjectStep资源,我必须创建一个API端点,该端点将用于更新我的ProjectStep以将其标记为完成并创建下一个ProjectStep。 In my REST API I could just do something like this : 在我的REST API中,我可以做这样的事情:

PATCH /project-mark/1
POST  /project-mark

But I would like to use only one request to update the current step, create a new one and return the newly created ProjectStep. 但是我只想使用一个请求来更新当前步骤,创建一个新请求并返回新创建的ProjectStep。

What method would you use? 您将使用哪种方法? A PATCH request updating an existing resource and returning a different resource doesn't sound like a good idea. PATCH请求更新现有资源并返回其他资源听起来不是一个好主意。

Thank's 谢谢

Use a PUT request, please see this link 使用PUT请求,请参见此链接

https://stackoverflow.com/questions/630453/put-vs-post-in-rest

Use PUT APIs primarily to update existing resource (if the resource does not exist then API may decide to create a new resource or not). 主要使用PUT API更新现有资源(如果资源不存在,则API可能会决定是否创建新资源)。 If a new resource has been created by the PUT API, the origin server MUST inform the user agent via the HTTP response code 201 (Created) response and if an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful completion of the request. 如果PUT API已创建新资源,则原始服务器务必通过HTTP响应代码201(已创建)响应通知用户代理,并且如果修改了现有资源,则应为200(确定)或204(无内容)应发送响应代码以指示请求已成功完成。

If the request passes through a cache and the Request-URI identifies one or more currently cached entities, those entries SHOULD be treated as stale. 如果请求通过缓存,并且Request-URI标识一个或多个当前缓存的实体,则应将这些条目视为过期。 Responses to this method are not cacheable. 此方法的响应不可缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM