How to pass body through `httpclient` delete request in Angular5

siva.kumar picture siva.kumar · Mar 26, 2018 · Viewed 7.4k times · Source

How to pass body through httpClient in delete request?

Please check my code. Is there any idea to pass data through body in delete request. There is no proper source how to call this request in angular 5.

let body = removeFile;
    return this.httpClient.delete(`${apiRoot}RemoveQueryData`, {
      headers: new HttpHeaders().set('Content-Type', 'application/json').set('Authorization', `Bearer ${accessToken}`),
      observe: removeFile
    })

That body I am passing through observe. It's throwing following error.

Error:

Error: Unreachable: unhandled observe type [object Object]}
    at HttpClient.request (http.js:1520)
    at HttpClient.delete (http.js:1546)

Answer

Gavishiddappa Gadagi picture Gavishiddappa Gadagi · Mar 26, 2018

For now Angular HttpClient doesn't support body in delete method you can post body throgh Request Options.

let body = removeFile;
let queryParams = new HttpParams().set('key', value); // key and value are both strings
let headerData = new HttpHeaders().set('Content-Type', 'application/json').set('Authorization', `Bearer ${accessToken}`);

return this.httpClient.delete(`${apiRoot}RemoveQueryData`, 

    new RequestOptions( {
      headers: headerData,  // optional
      params: queryParams,  // optional
      body: body,
      observe: 'response',  // optional
      responseType: 'response' // default type json ...
    })
   );