[英]REST API Design query
I have a problem in deciding what to do in this case in REST API design. 在决定在这种情况下如何在REST API设计中执行操作时遇到问题。
here is my problem, 这是我的问题,
I have a resource domain model, which has a nested object, which is also a domain model. 我有一个资源域模型,它有一个嵌套的对象,这也是一个域模型。
you can imagine something like this 你可以想象这样的事情
{
"name":"abc"
"type":{
"name":"typeName",
"description":"description"
}
}
Now, i want to be able to fetch the outer model resources, based on the inner model and few more params. 现在,我希望能够基于内部模型和更多参数来获取外部模型资源。
for example, i want to fetch all outer model resources which have a given type and some params like page number, size etc. 例如,我想获取具有给定类型和某些参数(例如页码,大小等)的所有外部模型资源。
So my questions, 所以我的问题
1.the API should accept inner model in post, and return outer model, is it a good rest design?
1. API应该在发布后接受内部模型,然后返回外部模型,这是一个好的设计吗?
- How do i pass the extra params?
如何传递额外的参数? It's a POST, can't put them in url, and can't put them inner model.
这是一个POST,不能将它们放在url中,也不能将它们放在内部模型中。
Should i create a new model, which contains these extra params and the type resource also? 我是否应该创建一个包含这些额外参数和类型资源的新模型? like
喜欢
{
"page":"10",
"type":{
"name":"typeName",
"description":"description"
}
}
If you are making a generic HTTP service, it's acceptable to use POST to send a complex query, and to get a response. 如果您要提供通用的HTTP服务,则可以使用POST发送复杂的查询并获得响应。
If you are trying to be RESTful, then this is a bad practice. 如果您尝试成为RESTful,那么这是一个不好的做法。 You have two options.
您有两个选择。 Option 1 is to find a way to encode your query in the URL, so you can use a GET request.
选项1是找到一种在URL中对查询进行编码的方法,以便您可以使用GET请求。
Option 2 is more involved. 选项2涉及更多。 I wouldn't necessarily say that I would suggest this, but it's a method to get around the constraints of REST while having complex queries.
我不一定要建议我这样做,但这是在进行复杂查询时解决REST约束的一种方法。
The idea is that you use POST
to create a 'query' resource. 想法是您使用
POST
创建“查询”资源。 Almost as if you doing a server-side prepared statement, and then later on use GET
to get the result of the query. 几乎就像您在执行服务器端的预处理语句一样,然后在以后使用
GET
获取查询结果。
Example of the client->server conversation: 客户->服务器对话的示例:
POST /queries
Content-Type: application/json
...
A response to this might be: 对此的响应可能是:
HTTP/1.1 201 Created
Location: /queries/1234
Link: </queryresults/1234>; rel="some-relationship-identifier"
Then after that you could do a GET
on /queries/1234 to see the query you 'prepared' and a GET
on /queryresults/1234
to see the actual report. 然后,您可以在/ queries / 1234上执行
GET
以查看您“准备”的查询,并在/queryresults/1234
进行GET
以查看实际的报告。
Benefits of this is that it stays within the constraints of REST, and that you could potentially re-use queries and take a longer time to generate the results. 这样做的好处是,它不受REST的限制,并且您可能潜在地重用查询并花费更长的时间来生成结果。
The obvious drawback is that it's harder to explain this to a consumer of your API, as not everyone might be familiar with this pattern and it's an extra HTTP request. 明显的缺点是,很难向您的API使用者解释这一点,因为不是每个人都可能熟悉此模式,并且这是一个额外的HTTP请求。
So you have to decide: 因此,您必须决定:
POST
for some complex queries. POST
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.