Restful way for deleting a bunch of items
In wiki article for REST it is indicated that if you use http://example.com/resources DELETE, that means you are deleting the entire collection.
If you use http://example.com/resources/7HOU57Y DELETE, that means you are deleting that element.
I am doing a WEBSITE, note NOT WEB SERVICE.
I have a list that has 1 checkbox for each item on the list. Once i select multiple items for deletion, i will allow users to press a button called DELETE SELECTION. If user presses the button, a js dialog box will popup asking user to confirm the deletion. if user confirms, all the items are deleted.
So how should i cater for deleting multiple items in a RESTFUL way?
NOTE, currently for DELETE in a webpage, what i do is i use FORM tag with POST as action but include a _method with the value DELETE since this is what was indicated by others in SO on how to do RESTful delete for webpage.
I think rojoca's answer is the best so far. A slight variation might be, to do away with the javascript confirm on the same page, and in stead, create the selection and redirect to it, showing a confirm message on that page. In other words:
From:
http://example.com/resources/
do a
POST with a selection of the ID's to:
http://example.com/resources/selections
which, if successful, should respond with:
HTTP/1.1 201 created, and a Location header to:
http://example.com/resources/selections/DF4XY7
On this page you will then see a (javascript) confirm box, which if you confirm will do a request of:
DELETE http://example.com/resources/selections/DF4XY7
which, if successful, should respond with: HTTP/1.1 200 Ok (or whatever is appropriate for a successful delete)
One option is to create a delete "transaction". So you POST
to something like http://example.com/resources/deletes
a new resource consisting of a list of resources to be deleted. Then in your application you just do the delete. When you do the post you should return a location of your created transaction eg, http://example.com/resources/deletes/DF4XY7
. A GET
on this could return the status of the transaction (complete or in progress) and/or a list of resources to be deleted.
Here's what Amazon did with their S3 REST API.
Individual delete request:
DELETE /ObjectName HTTP/1.1
Host: BucketName.s3.amazonaws.com
Date: date
Content-Length: length
Authorization: authorization string (see Authenticating Requests (AWS Signature Version 4))
Multi-Object Delete request:
POST /?delete HTTP/1.1
Host: bucketname.s3.amazonaws.com
Authorization: authorization string
Content-Length: Size
Content-MD5: MD5
<?xml version="1.0" encoding="UTF-8"?>
<Delete>
<Quiet>true</Quiet>
<Object>
<Key>Key</Key>
<VersionId>VersionId</VersionId>
</Object>
<Object>
<Key>Key</Key>
</Object>
...
</Delete>
But Facebook Graph API, Parse Server REST API and Google Drive REST API go even further by enabling you to "batch" individual operations in one request.
Here's an example from Parse Server.
Individual delete request:
curl -X DELETE
-H "X-Parse-Application-Id: ${APPLICATION_ID}"
-H "X-Parse-REST-API-Key: ${REST_API_KEY}"
https://api.parse.com/1/classes/GameScore/Ed1nuqPvcm
Batch request:
curl -X POST
-H "X-Parse-Application-Id: ${APPLICATION_ID}"
-H "X-Parse-REST-API-Key: ${REST_API_KEY}"
-H "Content-Type: application/json"
-d '{
"requests": [
{
"method": "POST",
"path": "/1/classes/GameScore",
"body": {
"score": 1337,
"playerName": "Sean Plott"
}
},
{
"method": "POST",
"path": "/1/classes/GameScore",
"body": {
"score": 1338,
"playerName": "ZeroCool"
}
}
]
}'
https://api.parse.com/1/batch
链接地址: http://www.djcxy.com/p/71444.html
上一篇: 从URL中输入标题和派生类型
下一篇: Restful的方式删除一堆物品