How to be RESTful with long URLs?
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.
Lutz Horn have answered it really well. But there is a good possibility that client might not be wishing to make three api calls - one to create a query response, second to get it, third to delete it.
If you are sure that your query length would remain less than 8 KB(which is the limit for get query length for most webservers), go ahead with GET request. Besides, if you are not interested in caching, use post there is nothing wrong with ignoring REST once in a while if you are well aware of the consequences.
链接地址: http://www.djcxy.com/p/42172.html上一篇: 为什么GET中字符的总量有限?
下一篇: 如何使用长URL进行RESTful?