I'm trying to talk to a somewhat RESTful API from an Angular 2 frontend.
To remove some item from a collection, I need to send some other data in addition to the removée unique id(that can be appended to the url), namely an authentication token, some collection info and some ancilliary data.
The most straightforward way I've found to do so is putting the authentication token in the request Headers, and other data in the body.
However, the Http module of Angular 2 doesn't quite approve of a DELETE request with a body, and trying to make this request
let headers= new Headers();
headers.append('access-token', token);
let body= JSON.stringify({
target: targetId,
subset: "fruits",
reason: "rotten"
});
let options= new RequestOptions({headers:headers});
this.http.delete('http://testAPI:3000/stuff', body,options).subscribe((ok)=>{console.log(ok)}); <------line 67
gives this error
app/services/test.service.ts(67,4): error TS2346: Supplied parameters do not match any signature of call target.
Now, am I doing something wrong syntax-wise? I'm pretty sure a DELETE body is supported per RFC
Are there better ways to send that data?
Or should I just dump it in headers and call it a day?
Any insight on this conundrum would be appreciated
Below is a relevant code example for Angular 4/5 with the new HttpClient.
Below is an example for Angular 6
The http.delete(url, options) does accept a body. You just need to put it within the options object.
Reference options interface: https://angular.io/api/http/RequestOptions
You are actually able to fool
Angular2 HTTP
into sending abody
with aDELETE
by using therequest
method. This is how:Note, you will have to set the request method in the
RequestOptionsArgs
and not inhttp.request
's alternative first parameterRequest
. That for some reason yields the same result as usinghttp.delete
I hope this helps and that I am not to late. I think the angular guys are wrong here to not allow a body to be passed with delete, even though it is discouraged.
In Angular 5, I had to use the request method instead of delete to send a body. The documentation for the delete method does not include body, but it is included in the request method.
The REST doesn't prevent body inclusion with DELETE request but it is better to use query string as it is most standarized (unless you need the data to be encrypted)
I got it to work with angular 2 by doing following: