Programing

많은 항목을 삭제하는 편안한 방법

lottogame 2020. 9. 3. 23:35
반응형

많은 항목을 삭제하는 편안한 방법


에서 REST에 대한 위키 기사 당신이 사용하는 경우 지적한다 http://example.com/resources 수단은 전체 컬렉션을 삭제하는 것을 삭제합니다.

http://example.com/resources/7HOU57Y DELETE 를 사용하는 경우 해당 요소를 삭제하는 것입니다.

웹 사이트를하고 있는데 웹 서비스가 아닙니다.

목록의 각 항목에 대해 1 개의 확인란이있는 목록이 있습니다. 삭제할 항목을 여러 개 선택하면 사용자가 DELETE SELECTION이라는 버튼을 누를 수 있습니다. 사용자가 버튼을 누르면 js 대화 상자가 팝업되어 사용자에게 삭제를 확인합니다. 사용자가 확인하면 모든 항목이 삭제됩니다.

그렇다면 RESTFUL 방식으로 여러 항목을 삭제하려면 어떻게해야합니까?

참고 현재 웹 페이지에서 삭제를 들어, 내가 무엇을 작업으로 POST와 내가 사용 FORM 태그하지만 이후 값으로 _method가 삭제 포함 이 편안하고는 웹 페이지에 대한 삭제하는 방법에 SO에 다른 사람에 의해 지시 된 것입니다 .


지금까지로 조카의 대답 이 최고 라고 생각 합니다. 약간의 변형이있을 수 있습니다. 동일한 페이지에서 자바 스크립트 확인을 없애고 대신 선택 항목을 만들고 리디렉션하여 해당 페이지에 확인 메시지를 표시합니다. 다시 말해:

출처 :
http://example.com/resources/

선택한 ID로 POST :
http://example.com/resources/selections

성공하면 다음과 같이 응답해야합니다.

HTTP / 1.1 201 생성 및 위치 헤더 :
http://example.com/resources/selections/DF4XY7

이 페이지에 (자바 스크립트) 확인 상자가 표시됩니다. 확인하면 다음을 요청합니다.

http://example.com/resources/selections/DF4XY7 삭제

성공하면 다음과 같이 응답해야합니다. HTTP / 1.1 200 Ok (또는 성공적인 삭제에 적합한 것)


한 가지 옵션은 삭제 "트랜잭션"을 만드는 것입니다. 그래서 당신 POSThttp://example.com/resources/deletes삭제할 자원 목록으로 구성된 새로운 자원 과 같은 것 입니다. 그런 다음 응용 프로그램에서 삭제를 수행하십시오. 게시를 할 때 생성 된 거래의 위치 (예 : http://example.com/resources/deletes/DF4XY7. 이에 대한 A GET는 트랜잭션의 상태 (완료 또는 진행 중) 및 / 또는 삭제할 리소스 목록을 반환 할 수 있습니다.


Amazon이 S3 REST API로 수행 한 작업은 다음과 같습니다.

개별 삭제 요청 :

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))

다중 객체 삭제 요청 :

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>           

그러나 Facebook Graph API , Parse Server REST APIGoogle Drive REST API 는 한 번의 요청으로 개별 작업을 "일괄 처리"할 수 있도록함으로써 더욱 발전합니다.

다음은 Parse Server의 예입니다.

개별 삭제 요청 :

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

일괄 요청 :

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

DELETE http://example.com/resources/id1,id2,id3,id4 또는 DELETE http://example.com/resources/id1+id2+id3+id4 라고 말합니다 . 이 위키피디아 기사를 인용하는 "REST는 아키텍처 (...) [아님] 프로토콜"이므로이를 수행하는 단일 방법이 없다고 생각합니다.

HTML이있는 JS 없이는 위의 작업이 불가능하다는 것을 알고 있지만 REST가 다음과 같은 느낌을받습니다.

  • 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.

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"


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.


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.


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.

참고URL : https://stackoverflow.com/questions/2421595/restful-way-for-deleting-a-bunch-of-items

반응형