简体   繁体   English

Rest API用于POST和GET / PUT的不同模型

[英]Rest API different models for POST and GET/PUT

I am designing a REST API. 我正在设计一个REST API。 As you know there is no real specification for a RESTful API. 如您所知,RESTful API没有真正的规范。 In my case i have a simple API where i have users. 在我的情况下,我有一个简单的API,我有用户。 I can get users, update them. 我可以得到用户,更新它们。 Something like this: 像这样的东西:

GET .../users/12 GET ... / users / 12

However for creating users i want the only to allow a subset of the properties. 但是,对于创建用户,我希望仅允许属性的子集。 Eg When you get a user, you get many properties like: Id, Name, CreationDate and so on 例如,当您获得用户时,您将获得许多属性,例如:Id,Name,CreationDate等

When you create a user i only want to allow to POST the Name and nothing else. 当您创建用户时,我只想允许POST名称而不是其他内容。 Is it allowed to have different models for different http verbs for the same resource in your understanding of a RESTful service? 在理解RESTful服务时,是否允许为同一资源使用不同的http动词的不同模型? Or would you expect that a 或者你会期待吗?

POST ../users POST ../users

can have exactly the same properties as you get when you fetch an existing one? 可以具有与获取现有属性时完全相同的属性?

Or is there something you think i should not do this? 或者你认为我不应该这样做吗? Just wondering after reading many religious discussions ;) 在阅读了许多宗教讨论后,只是想知道;)

Thank you in advance best Laurin 提前谢谢你最好的劳林

First: I think there is a good definition of the RESTful architecture style in Roy Fieldings dissertation Architectural Styles and the Design of Network-based Software Architectures . 第一:我认为在Roy Fieldings论文建筑风格和基于网络的软件架构设计中, RESTful架构风格有一个很好的定义。

Second: I think you have a small misunderstanding REST is not about models or controllers it is about resources. 第二:我认为你有一个小的误解REST不是关于模型或控制器它是关于资源。 Often there is a one-to-one relation between model and resource but it is not required. 模型和资源之间通常存在一对一的关系,但并不是必需的。

Third: To realize your requirements you only need one user model. 第三:要实现您的要求,您只需要一个user模型。 When the API user posts more than your allowed parameters just ignore them and use only the parameters you want to set on your newly created model. 当API用户发布超过您允许的参数时,请忽略它们,并仅使用您要在新创建的模型上设置的参数。 Or if you don't want to ignore them return a 400 Bad Request HTTP status. 或者,如果您不想忽略它们,则返回400 Bad Request HTTP状态。

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

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