Vijay
Vijay

Reputation: 418

How to get gzip data from the backend in angular 5 using httpclient

I am trying get the zipped file from the backend. Please find the code for httpclient as below :

I tried multiple options in header adding content-type with different options. But not able to come up with exact template. I couldn't find anything related to get the zipped data from the server in the angular api. Please let me know if any one would have come up with something similar issue.

getSpecialHeaders() {
    return new HttpHeaders({
         'Content-Encoding': 'gzip',
         'specialHeader': '',
         'Content-Type': 'gzip'
    });
}

getAllUnitsReport() {
 //Function to get the gzip data
const headersSpl = this.getSpecialHeaders();
//httpoptions with different options tried in content-type and accept encoding
const httpOptions = {
  headers : headersSpl };
return this.httpClient.get(envConfig.appURL.assetAdoption, httpOptions)
  .catch((error) => {
    return Observable.of(error);
  });}

I get the error message after http call is successful as below :

Http failure response for (unknown url): 0 Unknown Error

Upvotes: 2

Views: 4654

Answers (1)

Daniel
Daniel

Reputation: 11182

The Content-Encoding header is a response header that the server provides.

Usually the client provides a Accept-Encoding header to specify which content encoding you want to request.

For example: Accept-Encoding: gzip, deflate

See MDN on Compressing with gzip

However this specific header is marked as a forbidden header meaning it can't be set programmatically by Javascript that executes in a browser (still works in Node however).

Most browsers will automatically add a Accept-Encoding header containing all the content encodings that it supports.

Upvotes: 1

Related Questions