Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a clean way to send a body with DELETE request?

I need to send a request body with my DELETE requests using $resource

The only way I could see to do this was to change:

https://github.com/angular/angular.js/blob/master/src/ngResource/resource.js

From

var hasBody = action.method == 'POST' || action.method == 'PUT' || action.method == 'PATCH';

To

var hasBody = action.method == 'POST' || action.method == 'PUT' || action.method == 'PATCH' || action.method == 'DELETE';

Is there a better way to override this? Like when you alter the content type header you can do:

$httpProvider.defaults.headers["delete"] = {'Content-Type': 'application/json;charset=utf-8'};

Or something similar... Ive googled this but maybe Ive missed something obvious (not for the first time). Thanks for any help in advance.

like image 648
paullth Avatar asked Mar 01 '13 13:03

paullth


People also ask

Can you send body with delete request?

Yes it is allowed to include a body on DELETE requests, but it's semantically meaningless. What this really means is that issuing a DELETE request with a request body is semantically equivalent to not including a request body.

How do you send a delete request in REST API?

We shall first send a DELETE request via Postman on an endpoint − http://dummy.restapiexample.com/api/v1/delete/100. Using Rest Assured, we shall check if the response body contains the string Successfully! Record has been deleted.

Does a delete request need a body?

According to Mozilla a DELETE request "may" have a body, compared to a PUT, which should have a body. By this it seems optional whether you want to provide a body for a DELETE request.


2 Answers

This works.

$scope.delete = function(object) {
    $http({
        url: 'domain/resource',
        method: 'DELETE',
        data: {
            id: object.id
        },
        headers: {
            "Content-Type": "application/json;charset=utf-8"
        }
    }).then(function(res) {
        console.log(res.data);
    }, function(error) {
        console.log(error);
    });
};
like image 157
Simba Avatar answered Oct 12 '22 05:10

Simba


You can inject the $http (http://docs.angularjs.org/api/ng.%24http#Usage) component into one of one of your controllers and by using it as follows :

$http({method: 'DELETE', url: 'www.url.com', headers: {'X-MY-HEADER': 'MY_VALUE'}});

I hope this what you expected.

like image 22
Halim Qarroum Avatar answered Oct 12 '22 06:10

Halim Qarroum