0

I don't know but this seems ambiguous to me. I'm trying to make a DELETE request, applied on multiple objects, referenced with their IDs (the ones in the array).

This is my controller :

    [HttpDelete]
    public void RemoveRoomWithDevices([FromUri]int roomId, [FromBody] int[] userDevicesId)
    { //code
    }

This is the JS :

    function DeleteRoomWithDevices(RoomID, FloorId, userDevicesID) {
    $.ajax({
        url: "/api/Room/RemoveRoomWithDevices?RoomId=" + RoomID,
        type: "DELETE",
        dataType: 'json',
        contentType: 'application/json',
        data: {
            userDevicesId: userDevicesID
        },
        traditional: true,
        success: function (data) {//success}
    });

Here, in the POST column, the array is not passed (I'm debugging with firebug). When I replace the type with type: "POST" I am able to see the array but I can't execute the delete query.

What do you suggest?

Sahar Ch.
  • 489
  • 1
  • 8
  • 28
  • I'm not sure, but I think that `DELETE` should not contain request body. It's not dissallowed in the spec (http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html) but I've read something in past that controller just ignores it. Why you cant execute delete query in POST? What's more, you can pass and array as URL - if such an array is not big. –  Jul 24 '14 at 11:42
  • Yes, well when I did it I got this error {"Message":"The requested resource does not support http method 'POST'."} – Sahar Ch. Jul 24 '14 at 11:46
  • 2
    Nice discussion about DELETE body: http://stackoverflow.com/questions/14323716/restful-alternatives-to-delete-request-body. How to pass array parameter in GET (and DELETE): http://stackoverflow.com/questions/9981330/how-to-pass-an-array-of-integers-to-a-asp-net-web-api-rest-service – Ilya Luzyanin Jul 24 '14 at 11:47
  • @Sahar Ch changing type `DELETE` to `POST` is not enough - you should change action attribute from `HttpDelete` to `HttpPost` :) –  Jul 24 '14 at 11:49
  • Ah yesss! How did I miss that! I'll give it a try thanks – Sahar Ch. Jul 24 '14 at 11:50

2 Answers2

0

Pass both parameters with data and their name should match parameters of the method:

$.ajax({
        url: "/api/Room/RemoveRoomWithDevices",
        type: "DELETE",
        dataType: 'json',
        contentType: 'application/json',
        data: {
            roomId: RoomID,
            userDevicesId: userDevicesID
        },
        traditional: true,
        success: function (data) {//success}
    });
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0

"The spec does not explicitly forbid or discourage http body" Is an entity body allowed for an HTTP DELETE request?

For my own opinion delete request shouldn't contain any body as well as have to point exactly one resource for deletion (/cars for all collection /cars/{id} for particular entity) Batch deletion is not obvious topic you can look at the following topic about it

Patterns for handling batch operations in REST web services?

Community
  • 1
  • 1
Victor
  • 618
  • 4
  • 12