I struggled with the HTTP patch that Angular provides, noticed that the method patch(url, body, options)
- which has a lot of overloaded functions - only accepts hard coded parameter for HTTP options.
Hard coded example which works:
patchEntity(id: number): Observable<HttpResponse<string>> {
const url: string = `url-to-resource`;
const body: string = `[{"op":"replace","path":"/id","value":345}]`;
return this.httpClient.patch(url, body, {
headers: new HttpHeaders({'Content-Type': 'application/json-patch+json'}),
observe: 'response',
responseType: 'text'
});
}
Outsourced HTTP options that is responding with the following error message:
Type Observable<ArrayBuffer> is not assignable to type 'Observable<HttpResponse<string>>
Type 'ArrayBuffer' is missing the following properties from type 'HttpResponse<string>': body, type, clone, headers, and 4 more.
patchEntity(id: number): Observable<HttpResponse<string>> {
const url: string = `url-to-resource`;
const body: string = `[{"op":"replace","path":"/id","value":345}]`;
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json-patch+json'}),
observe: 'response',
responseType: 'text'
}
return this.httpClient.patch(url, body, httpOptions); // <--- errors occurs here
}
Seems, I'm missing some basic understanding of what's going on in Typescript or Angular.
Can somebody teach me why Angular do not let me outsourcing the HTTP options and the errors occur?
Thanks in advance
Observable<HttpResponse<string>>
, howeverthis.httpClient.patch(url, body, httpOptions)
instead returnsObservable<ArrayBuffer>
. Either remove your type, update the type, or modify the the resulting value ofpatch()
using something likepipe
andmap
to transform into an Observable of a string. So the error is not related to options, it's related to the return type you specified for the method.