1

issue here with the delete request (in angular 4), as i get a

requestheaders does not exist in the type requestoptionargs

below is my code:

myMethod(id: string, reason: string): Observable<any> {

        const reqHeaders = new Headers({
            'Content-Type': 'application/json'
          });
        const body =
            `{"reqId": "${id}",
             "reqReason": "${reason}"
            }`;
        return this.httpClient.delete(
            `http://localhost:188/.../1677777555`,
            new RequestOptions({ reqHeaders, body }))
            .map(rez => JSON.parse(rez))
            // .catch(error => this.handleError(error))
            ;
    }

on this line is where i get the error new RequestOptions({ requestHeaders, body })) Any obvious faults here?

Edit: another variant

Same body as before and:

const options = new RequestOptionsArgs({
    body: body,
    method: RequestMethod.Delete
  });

return this.httpClient.request('http://testAPI:3000/stuff', options);
}

now I get a:

RequestOptionsArgs refers to a type but is beingused as a value here

annepic
  • 1,147
  • 3
  • 14
  • 24

2 Answers2

3

Try this one:

import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';

myMethod(id: string, reason: string {
    let params = new HttpParams()
        .append('id', id)
        .append('reason', reason)
    let headers = new HttpHeaders({ 'Content-Type': 'application/JSON' });
    return this.httpClient.delete<any>('http://localhost:188/.../1677777555', { headers: headers, params: params })
    .map(rez => JSON.parse(rez));;
Joe Belladonna
  • 1,349
  • 14
  • 20
  • Expected 1-2 arguments, but got 3. According to angular4, the delete should have 2 arguments. The second is options that may contain the body and headers. https://angular.io/api/common/http/HttpClient#delete I also tried based on this the following (will edit my original post in 5 min). Thanks Joe – annepic Apr 05 '18 at 06:37
  • @annepic I made an error check my answer now, I have corrected it – Joe Belladonna Apr 05 '18 at 06:44
3

as per documentation try like this : https://angular.io/api/common/http/HttpClient#members

can you tryout general request like this

let options = new RequestOptionsArgs({ 
    body: body,
    method: RequestMethod.Delete
    headers : reqHeaders
  });

this.http.request('http://testAPI:3000/stuff', options)
    .subscribe((ok)=>{console.log(ok)});
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • Thank yoiu Pranay, I get this: `Argument of type '{ headers: HttpHeaders; body: string; }' is not assignable to parameter of type RequesrOptionArgs | undefined. ` – annepic Apr 05 '18 at 06:34
  • Your answer pointed me to the right direction. With Angular 6, got it working with `this.http.request('DELETE', url, { body: myObjOrArrayToBody })` – Cesar Oct 09 '18 at 16:14