Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $http.delete Request with Body and Headers

Hey so for POST/PUT requests simply doing

$http.post(url, body, headers)

worked fine

But with DELETE it gets my body, but completely ignores my headers...

$http.delete(url, body, headers)
like image 904
Patrick Duncan Avatar asked Jun 24 '16 16:06

Patrick Duncan


People also ask

Can we send body in http delete request?

Sending a message body on a DELETE request might cause some servers to reject the request. But you still can send data to the server using URL parameters. This is usually an ID of the resource you want to delete.

How do you send a body in delete request in angular 9?

In Angular 9+ you will need to this. http. request('DELETE', 'url', { body: ... }) . In my case the uppercase method string was necessary.

Which of the following is the correct syntax for making a get call and handling the error in angular?

get(url) . then(function (response) { console. log('get',response) }) . catch(function (data) { // Handle error here });


2 Answers

The documentation is terrible, with v1.3.20 you need to do:

$http.delete(url, {data: {...}, headers: {...}})

... which is completely different than post/put for some reason.

like image 63
Patrick Duncan Avatar answered Nov 11 '22 14:11

Patrick Duncan


The two key elements to include in your options are the data an Content-Type (as part of your headers object). Your request would look something like:

$http.delete(url, {data: {...}, headers: {'Content-Type': 'application/json;charset=utf-8'}})

Credit goes to @Harshit Anand for his on another SO post.

like image 20
astangelo Avatar answered Nov 11 '22 12:11

astangelo