Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve gzipped content for AJAX requests with Google App Engine?

I'm building an API for my service hosting on Google App Engine. This API will serve large amounts of data so I want it gzipped. I found the section in the docs on how to get GAE to gzip data is by setting "User-Agent" and "Accept-Encoding" both to "gzip". Tested this, works fine (though seems kinda hacky...).

https://developers.google.com/appengine/docs/python/#Responses

But one of the clients being built to use the API is running in a browser, and there appears to be no way for an XMLHttpRequest to set those headers, at least in Chrome. I get errors when I try:

Refused to set unsafe header "User-Agent"
Refused to set unsafe header "Accept-Encoding"

What, if anything, can a Javascript client do to get an automatically gzip encoded response from Google App Engine for an XMLHttpRequest, and have it automatically decoded by the browser? Is this even possible? I would assume AJAX requests can decode gzip content automatically if other kinds of browser requests can. But the only solution I can see is the server will have to encode the response manually and the browser client will have to decode it manually, but that seems pretty sub-optimal.

I found these answers already but they don't seem to offer any solution:

App Engine Accept-Encoding JQuery Ajax Request: Change User-Agent

like image 378
scott_at_skritter Avatar asked Dec 21 '25 06:12

scott_at_skritter


1 Answers

You shouldn't need to adjust these. The browser should set these headers on its own. If the browser is not setting an Accept-Encoding header for gzip, it may be that the browser doesn't support gzip, in which case it won't automatically decode.

The short answer is, essentially you need to do nothing.

like image 81
dragonx Avatar answered Dec 22 '25 18:12

dragonx