Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular gzip json file automatically : Refused to set unsafe header "Accept-Encoding"

I use a gzip json static file on my server (actually, it's my test server) and the data i receive is always compressed.

Here is my code :

    $http({ 
      url :'app/assets/json/makes2v.json.gz',
      method: "GET",
      headers: { 'Accept-Encoding': 'gzip' }})
    .success(function(data, status, headers, config) {
      console.log(data);
  });

I use angular 1.3.15 and Chrome.

In the console I have this error:

Refused to set unsafe header "Accept-Encoding"

Any help will be appreciated.

Thanks,

like image 886
Master Mind Avatar asked Sep 08 '15 17:09

Master Mind


People also ask

How do I disable gzip content Encoding?

The solution would be to deactivate gzip encoding in the portal and perform any necessary gzip encoding through the HTTP server. However, the gzip encoding cannot be deactivated through a central portal property.

How do I add a accept-Encoding header?

To check this Accept-Encoding in action go to Inspect Element -> Network check the request header for Accept-Encoding like below, Accept-Encoding is highlighted you can see.

What does accept-Encoding GZIP mean?

The Accept-Encoding header is used for negotiating content encoding. Accept-Encoding: gzip, deflate. The server responds with the scheme used, indicated by the Content-Encoding response header. Content-Encoding: gzip. Note that the server is not obligated to use any compression method.

What is HTTP accept-Encoding?

The Accept-Encoding request HTTP header indicates the content encoding (usually a compression algorithm) that the client can understand. The server uses content negotiation to select one of the proposals and informs the client of that choice with the Content-Encoding response header.


1 Answers

You shouldn't set the request header "Accept-Encoding". This is set automatically by the browser. See this Q&A for a list a browsers that accept gzip.

On the server side, you will need to set the Content-Encoding to:

Content-Encoding: gzip

Then the response should be automatically decompressed by the browser.

like image 191
tokafew420 Avatar answered Oct 02 '22 05:10

tokafew420