Restful way for deleting a bunch of items

后端 未结 8 1100
栀梦
栀梦 2020-12-04 05:26

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://ex

相关标签:
8条回答
  • 2020-12-04 05:47

    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
    
    0 讨论(0)
  • 2020-12-04 05:51

    I would say DELETE http://example.com/resources/id1,id2,id3,id4 or DELETE http://example.com/resources/id1+id2+id3+id4. As "REST is an architecture (...) [not] protocol" to quote this wikipedia article there is, I believe, no single one way of doing this.

    I am aware that above is not possible without JS with HTML but I get the feeling that REST was:

    • Created without thinking of minor details like transactions. Who would need to operate on more then single item? This is somehow justified in HTTP protocol as it was not intended to serve through it anything else other then static webpages.
    • Not necessary well adjusting into current models - even of pure HTML.
    0 讨论(0)
  • 2020-12-04 05:51

    Interestingly, i think the same method applies as to PATCHing multiple entities, and requires thinking about what we mean with our URL, parameters and REST method.

    1. return all 'foo' elements:

      [GET] api/foo

    2. return 'foo' elements with filtering for specific ids:

      [GET] api/foo?ids=3,5,9

    Wherein the sense is the URL and the filter determine "what elements we are dealing with?", and the REST method (in this case "GET") says "what to do with those elements?"

    1. Hence PATCH multiple records to mark them as read

      [PATCH] api/foo?ids=3,5,9

    ..with the data foo[read]=1

    1. Finally to delete multiple records, this endpoint is most logical:

      [DELETE] api/foo?ids=3,5,9

    Please understand I don't believe there are any "rules" on this - to me it just "makes sense"

    0 讨论(0)
  • 2020-12-04 05:52

    I had the same situation to delete multiple items. This is what I ended up doing. I used DELETE operation and the ids of items which were to be deleted were part of HTTP header.

    0 讨论(0)
  • 2020-12-04 05:59

    As Decent Dabbler answer and rojocas answer says, the most canonical is using virtual resources to delete a selection of resources, but I think that is incorrect from a REST perspective, because executing a DELETE http://example.com/resources/selections/DF4XY7 should remove the selection resource itself, not the selected resources.

    Taking the Maciej Piechotka anwser or the fezfox answer, I only have an objection: There's a more canonical way of pass an array of ids, and is using the array operator:

    DELETE /api/resources?ids[]=1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d&ids[]=7e8f9a0b-1c2d-3e4f-5a6b-7c8d9e0f1a2b

    In this way you are attacking to the Delete Collection endpoint but filtering the deletion with a querystring in the right way.

    0 讨论(0)
  • 2020-12-04 06:03

    As there is no 'proper' way to do this, what I have done in the past is:

    send DELETE to http://example.com/something with xml or json encoded data in the body.

    when you receive the request, check for DELETE, if true, then read the body for the ones to be deleted.

    0 讨论(0)
提交回复
热议问题