I encountered a challenge with using Angular's HTTP patch method and noticed that the overloaded function patch(url, body, options)
only accepts hardcoded values for HTTP options.
An example of a hardcoded approach that 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'
});
}
When trying to outsource the HTTP options, an error is triggered with the 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
}
It appears there may be a lack of understanding in either Typescript or Angular causing these issues.
Can anyone explain why Angular does not allow outsourcing HTTP options and why these errors are happening?
Appreciate any insights in advance