Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Body of Http.DELETE request in Angular2

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

like image 374
TriTap Avatar asked Aug 07 '16 23:08

TriTap


People also ask

Does http delete have body?

DELETE can have a body`. RFC 7231 section 4.3. 5 finalizes the language from version 26 with A payload within a DELETE request message has no defined semantics . So the body is allowed.

Can you delete request body in method?

This is an know scenario and Integration Server doesn't accept request body for HTTP DELETE Method. The only means to accept the content for DELETE method in IS is by passing the Query parameter.

What is HTTP delete request?

The HTTP DELETE method is used to delete a resource from the server. Unlike GET and HEAD requests, the DELETE requests may change the server state. 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.

What is the return type of HttpClient post delete request?

HttpClient is one of the best APIs to make HTTP requests. It returns an Observable, but if you want to return a Promise that can also be done using HttpClient. It is best practice to return an Observable and subscribe it in other functions.


2 Answers

If you use Angular 6 we can put body in http.request method.

Reference from github

You can try this, for me it works.

import { HttpClient } from '@angular/common/http';  @Component({   selector: 'app-root',   templateUrl: './app.component.html',   styleUrls: ['./app.component.scss'], }) export class AppComponent {    constructor(     private http: HttpClient   ) {     http.request('delete', url, {body: body}).subscribe();   } } 
like image 21
Druta Ruslan Avatar answered Sep 26 '22 15:09

Druta Ruslan


The http.delete(url, options) does accept a body. You just need to put it within the options object.

http.delete('/api/something', new RequestOptions({    headers: headers,    body: anyObject })) 

Reference options interface: https://angular.io/api/http/RequestOptions

UPDATE:

The above snippet only works for Angular 2.x, 4.x and 5.x.

For versions 6.x onwards, Angular offers 15 different overloads. Check all overloads here: https://angular.io/api/common/http/HttpClient#delete

Usage sample:

const options = {   headers: new HttpHeaders({     'Content-Type': 'application/json',   }),   body: {     id: 1,     name: 'test',   }, };  this.httpClient   .delete('http://localhost:8080/something', options)   .subscribe((s) => {     console.log(s);   }); 
like image 64
n4nd0_o Avatar answered Sep 26 '22 15:09

n4nd0_o