Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass responseType: 'blob' via post request in Angular

Tags:

angular

I am passing responseType: 'blob' via get request. It works well.

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { RequestOptions, Response, ResponseContentType } from '@angular/http';


let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/pdf');

return this.http.get(url, {
    headers: headers,
    responseType: 'blob'
}

How to pass same responseType via post request?

I tried :

const headers = new Headers({ 
    'Content-Type': 'application/pdf'
});

const options = new RequestOptions({headers: headers});
options.responseType = ResponseContentType.Blob;

return this.http.post(url, body, options)

but it doesnt work. I have error message: Argument of type '{ headers: Headers; }' is not assignable to parameter of type 'RequestOptionsArgs'. Types of property 'headers' are incompatible. Type 'Headers' is not assignable to type 'Headers'.

UPD Following comments bellow i've remade request:

   const headers = new HttpHeaders({ 'Content-Type': 'application/pdf'});
    return this.http.post(url, body, { headers, responseType:'blob' })

It works well! Thanks a lot!

like image 666
Lex Avatar asked Oct 19 '25 10:10

Lex


2 Answers

Use

responseType:'blob' as 'json'

References:

  • Discussion on Angular Github
  • Stackoverflow
like image 163
Suren Konathala Avatar answered Oct 22 '25 02:10

Suren Konathala


You need to use is as json,

responseType:'blob' as 'json'
like image 44
Parinda Rajapaksha Avatar answered Oct 22 '25 03:10

Parinda Rajapaksha