Where to set responseType to http GET for HttpClient in angular

10,018

You can specify that the data to be returned is not a JSON using the responseType. See the Requesting non JSON data

In your example, you should be able to use:

return this.http.get(
    `${environment.apiBaseUrl}/blah`, { responseType: 'text' })

EDIT

You can set the responseType as blob,

return this.http.get(`${environment.apiBaseUrl}/blah`, { responseType: 'blob' });
Share:
10,018

Related videos on Youtube

Drew13
Author by

Drew13

I'm an associate software developer six months out of college. My main focus in school was Java, but I had some experience building an application using PHP. Now I'm mainly working on building an Angular2 UI.

Updated on June 12, 2022

Comments

  • Drew13
    Drew13 about 2 years

    I have a service that makes an http call to my backend:

    exportSubs(param: Param): Observable<Sub[]> {
        return this.http.get<Sub[]>(
          `${environment.apiBaseUrl}/blah`,
          {headers: this.httpUtil.getReqHeaders})
          .catch(error => this.httpUtil.handleError(error));
      }
    

    where do I set responseType?

    • mast3rd3mon
      mast3rd3mon almost 6 years
      in the headers like normal
    • jonrsharpe
      jonrsharpe almost 6 years
      @mast3rd3mon that's absolutely not what you normally do! Do you mean in the same object as the headers?
    • jonrsharpe
      jonrsharpe almost 6 years
      Did you read the docs? They show exactly where to set it.
    • mast3rd3mon
      mast3rd3mon almost 6 years
      @jonrsharpe yes it is what you do, its the Accept header, unless he means what the angular http response type is, in which case it is already set to an array of Sub
    • jonrsharpe
      jonrsharpe almost 6 years
      @mast3rd3mon I think you misunderstand the question, I'd suggest also reading the docs I just linked.
    • mast3rd3mon
      mast3rd3mon almost 6 years
      i dont think i do misunderstand at all
    • jonrsharpe
      jonrsharpe almost 6 years
      @mast3rd3mon well you're talking about the Accept header and the OP is talking about the Angular HttpClient's responseType option, so...
  • jonrsharpe
    jonrsharpe almost 6 years
    That's not even valid syntactically, so I doubt you tried it.
  • Drew13
    Drew13 almost 6 years
    then I get error: "Expected 1-2 arguments, but got 3."
  • mast3rd3mon
    mast3rd3mon almost 6 years
    @Drew13 sounds like youre passing an extra parameter into the call, get methods only take 2 paramaters, the url and optional http options
  • Drew13
    Drew13 almost 6 years
    @mast3rd3mon yeah I had to get rid of where I set my headers. I need my responseType to be of type 'blob'. I'll just figure it out
  • Sajeetharan
    Sajeetharan almost 6 years
    you can set the type as blob
  • Drew13
    Drew13 almost 6 years
    @Sajeetharan that gives me Argument of type '{reponseType: 'blob;'} is not assignable to parameter type '{ headers?: HttpHeaders | {[header: string]: string | string[]: }; observe?: "body"; params?: Type '"blob"' is not assignable to type '"json"'
  • JB Nizet
    JB Nizet almost 6 years
    @Drew13 you need to read the API documentation, and the http guide. If the response type is a blob, there's no way the generic type can be Sub[]: your request is supposed to return a blob, not a JSON array of Subs.
  • Bravo
    Bravo over 2 years
    is there anyway I can set { responseType: 'blob' } and { responseType: 'text ' } together, as my error response is not able to work with blob