简体   繁体   English

REST API用于自定义参数

[英]REST API for custom parameter

I have an API method which supports a DELETE action: 我有一个支持DELETE动作的API方法:

api/jobs/:id

Once of the fields on the Job model is a reference value (not the same as the :id value). Job模型上的字段中有一个是reference值(与:id值不同)。

I need to be able to expose another DELETE action that would allow a 3rd party to remove a Job by the reference field. 我需要能够公开另一个DELETE动作,该动作将允许第三方通过引用字段删除作业。 The 3rd party would have no knowledge of the Job :id and thus need to use the reference field which they do have knowledge of. 第三方将不了解Job :id ,因此需要使用他们确实了解的reference字段。

What would be the best way to expose this? 揭露这一点的最佳方法是什么? How should I handle this route? 我应该如何处理这条路线?

Thanks! 谢谢!

I think your question is not about the implementation but about the style of the RESTful architecture, otherwise fill free to correct me... 我认为您的问题不是关于实现,而是关于RESTful体系结构的样式,否则请自由地纠正我...
So basically the comment of Buddy Yaussy is totally right. 因此,基本上,Buddy Yaussy的评论是完全正确的。 You have two options: 您有两种选择:

1) Access the entity via two different identifiers 1)通过两个不同的标识符访问实体

GET/DELETE api/externaljobs/:reference

and

GET/DELETE api/jobs/:id

In a RESTful API every entity must have at least one identifier to be addressable, so having two is no problem at all. 在RESTful API中,每个实体必须至少具有一个可寻址的标识符,因此拥有两个标识符完全没有问题。

2) Let the external client search for the right job 2)让外部客户搜索合适的工作

Depending on the technical and business restrictions you have, you could dictate that the external client has to search for the right job before he is able to delete it. 根据您的技术和业务限制,您可以决定外部客户端必须先搜索正确的作业,然后才能删除该作业。

this could work via 这可以通过

GET api/jobs?reference=1337

get the result: 得到结果:

<job>
    [...]
    <id>1234</id>
</job>

and with the result of that call 并与那个电话的结果

DELETE api/jobs/1234

(you could also just allow DELETE on job collections, so the external client would be able to call DELETE api/jobs?reference=1337 and would not have two send two calls) (您也可以只允许在作业集合上进行DELETE ,因此外部客户端将能够调用DELETE api/jobs?reference=1337并且不会有两个发送两个调用)


I personally like the second approach much more, because it looks much cleaner because everything you want is possible with basic HTTP methods and you still have just one representation of the job entity, but both of them are acceptable in a RESTful API and depend on "REST design taste" style I think. 我个人更喜欢第二种方法,因为它看起来更简洁,因为您想要的所有内容都可以使用基本的HTTP方法实现,并且您仍然只具有工作实体的一种表示形式,但是这两种方法在RESTful API中都是可以接受的,并且取决于“我认为REST设计具有品味。

Here are three other SO question to look into: 这是另外三个要研究的SO问题:
RESTful API behavior for entitys with two independent primary keys 具有两个独立主键的实体的RESTful API行为
REST API DESIGN - Getting a resource through REST with different parameters but same url pattern REST API设计-通过REST获取具有不同参数但URL模式相同的资源
Different RESTful representations of the same resource 相同资源的不同RESTful表示形式

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

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