Search code examples
c#angularasp.net-web-apitypescriptangular2-http

Angular 2 Http delete with Headers for consuming with Web API REST


I used Angular 2 Release and using Http and Headers from @angular/http.

I try call verb delete, with Headers, and the others verbs POST, GET and PUT, all right. But with "DELETE", don't work.

My code :

remove(url:string, id:any) {
   let headers = new Headers();
   headers.append('Authorization': 'Bearer ' + this.token);
   return this.http.delete(url, {
        headers: headers;
        body: { id: id }
   }).map(response => response.json());
}

A response received is: the requested resource does not support http method 'delete'.

In my controller I have some like this:

[HttpDelete]
public Task<HttpResponseMessage> Delete(int id)
{
    //..
}

Thanks for help me, regards !!


Solution

  • The solution that worked for me was, use query string, in this way:

    remove(urlController:string, id:any){
       let headers = new Headers();
       headers.append('Authorization': 'Bearer ' + this.token);
       return this.http.delete(urlController + '/?id=' + id, {
        headers: headers;
        }).map(response => response.json());
    }
    

    example:

    remove(urlController:string, id:any){
       let headers = new Headers();
       headers.append('Authorization': 'Bearer ' + this.token);
       return this.http.delete('users'+ '/?id=' + '2', {
        headers: headers;
        }).map(response => response.json());
    }