In an angular app that uses HttpClient service, The code below works fine:
this.http.get(myUrl, {responseType: 'json'})
I need to send the responseType as a dynamic parameter, something like:
this.http.get(myUrl, { responseType: (condition ? 'json' : 'blob') })
or like:
let options = { responseType: 'json' };
if (condition)
options = { responseType: 'blob' };
this.http.get(myUrl, options)
but typescript does not like it, and shows an error: No overload matches this call
What am I doing wrong and how can I achieve this?
Thanks!
Note: This Type "json" | undefined not satisfied by "json" in Angular HttpClient
does not meet my requirement, but helps me to correct the code:
const options: { responseType: "json" } = { responseType: "json" };
const optionsBlob: { responseType: "blob" } = { responseType: "blob" };
this.http.get(myUrl, condition ? options : optionsBlob)
yet, compiler is not satisfied, and shows the previous error.