如何使用长URL进行RESTful? [英] How to be RESTful with long URLs?
问题描述
REST架构说资源状态也应该得到一个URL。
我正在制作一个具有非常大的查询的小应用程序。我想为每个查询生成URL,但查询通常大于URL字符限制。我试过使用URL shorteners,但是我的URLs对于他们来说太大了。
很多网上的人都在建议使用POST,但这不是幂等的。我真的不想限制我的查询长度,并且URL应该能够识别无限量的资源。
是否有一种RESTful方法可以解决非常大的URL问题这不涉及POST请求?
要以RESFtul方式对此进行建模,请将查询视为资源。它们可以被创建,检索并最终删除。
客户端向<$ c发送 POST
请求$ c>查询资源与请求正文中的查询详细信息。
POST / queries
Content-Type:application / json
{
condition1:
{
on:field1,
比较 :等于,
值:42
},
condition2:
{
on:field2,
比较:like,
value:foo%
}
}
这将创建一个新的查询资源。服务器将回应:
201创建
位置:/ queries / D560EC80-1006-11E5-80F6-75919330F945
路径段 D560EC80-1006-11E5-80F6-75919330F945
将是服务器为这个特定查询生成的一个ID。
GET / queries / D560EC80-1006-11E5-80F6-75919330F945
服务器响应查询结果。
200 OK
Content-Type: application / json
{
id:D560EC80-1006-11E5-80F6-75919330F945,
querydetails:
{
条件1:
{
on:field1,
比较:等于,
值:42
},
condition2:
{
on:field2,
比较:like,
value:foo%
}
},
结果:
{
len gth:12,
results:
[
{
//有关第一次点击
的详细信息,
//更多点击
$ b $ / code>
稍后,客户端可以删除查询。
DELETE / queries / D560EC80-1006-11E5-80F6-75919330F945
或者服务器可以在一段时间后自动删除查询。
REST architecture says that a resources state also should get a URL.
I am making a small application with very large queries. I would like to generate URLs with each query, but the queries are often larger than the URL character limit. I tried using URL shorteners but my URLss are too big even for them.
A lot of people online are suggesting using POST, but that is not idempotent. I really do not want to limit my query length and URLs should be able to identify an infinite amount of resources.
Is there a RESTful way to get around having very large URLs that does not involve POST requests?
To model this in a RESFtul way consider queries to be resources. They can be created, retrieved, and eventually deleted.
The client makes a POST
request to a queries
resource with the query details in the request body.
POST /queries
Content-Type: application/json
{
"condition1":
{
"on": "field1",
"comparison": "equals",
"value": 42
},
"condition2":
{
"on": "field2",
"comparison": "like",
"value": "foo%"
}
}
This creates a new query resource. The server will respond:
201 Created
Location: /queries/D560EC80-1006-11E5-80F6-75919330F945
The path segment D560EC80-1006-11E5-80F6-75919330F945
will be an ID generated by the server for this specific query.
Then the client requests the state of this query resource.
GET /queries/D560EC80-1006-11E5-80F6-75919330F945
The server responds with the query result.
200 OK
Content-Type: application/json
{
"id": "D560EC80-1006-11E5-80F6-75919330F945",
"querydetails":
{
"condition1":
{
"on": "field1",
"comparison": "equals",
"value": 42
},
"condition2":
{
"on": "field2",
"comparison": "like",
"value": "foo%"
}
},
"result":
{
"length": 12,
"results":
[
{
// details about the first hit
},
// more hits
]
}
}
Later, the client can delete the query.
DELETE /queries/D560EC80-1006-11E5-80F6-75919330F945
Or the sever can automatically delete the query after some time.
这篇关于如何使用长URL进行RESTful?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!