Mikola
Mikola

Reputation: 9326

Removing HTTP headers from an XMLHttpRequest

I am working on an ajax long polling type application, and I would like to minimize the amount of bandwidth I am using. One of the big costs right now are the client side HTTP headers. Once I have a connection established and a session id stored on the client, I don't really want to squander any more bandwidth transferring redundant http information (such as browser type, accept encodings, etc.). Over the course of many connections, this quickly adds up to a lot of data!

I would really like to just take my XMLHttpRequest and nuke all of the headers so that only the absolute minimum gets transmitted to the server. Is it possible to do this?

Upvotes: 2

Views: 12509

Answers (3)

Sripathi Krishnan
Sripathi Krishnan

Reputation: 31528

You have very little control over request headers, but you can still do a few things -

  1. Reduce the size of the cookie. In general, you only want the session id, everything else can be eliminated and stored server side.
  2. Minimize http referrer by keeping a short URL. The longer your page url, the more data will have to be sent via the http referrer. One trick is to store data in the fragment identifier (the portion of the url after the #). The fragment identifier is never sent to the server, so you save a few bytes over there.
  3. Some request headers are only sent if you had previous set corresponding response headers. For example, you can indirectly control the ETag and if-modified-since request headers.

You may want to consider Web Sockets. Support is pretty good (IE10+).

Upvotes: 3

Ruslan Talpa
Ruslan Talpa

Reputation: 561

I think it's possible to remove all headers at least in some browsers. Take a look at the communication between gmail/calendar apps and the backend from google in chrome (it's not the same in firefox) it's possible google has some hidden api for the XMLHttpRequest object, you'll see something like the below output (notice there is no request headers section):

Request URL:https://mail.google.com/mail/u/0/channel/bind?XXXXXXXXXXXXXX
Request Method:POST
Status Code:200 OK

Query String Parameters
OSID:XXXXXXXXXXXXX
OAID:XXXXXXXXX
VER:8
at:XXXXXXXXXXXXXX
it:30
SID:XXXXXXXXXXXX
RID:XXXXXXXXX
AID:XXXXXXXXXX
zx:XXXXXXXXXXXX
t:1

Request Payload
count=1&ofs=211&req0_type=cf&req0_focused=1&req0__sc=c

Response Headers
cache-control:no-cache, no-store, max-age=0, must-revalidate
content-encoding:gzip
content-type:text/plain; charset=utf-8
date:Tue, 09 Oct 2012 08:52:46 GMT
expires:Fri, 01 Jan 1990 00:00:00 GMT
pragma:no-cache
server:GSE
status:200 OK
version:HTTP/1.1
x-content-type-options:nosniff
x-xss-protection:1; mode=block

Upvotes: 0

Anomie
Anomie

Reputation: 94794

You may be able to override some of the standard headers using setRequestHeader() before sending the request, but it is possible the browser may not allow overriding of some and it seems there is no way to get a list of headers (besides asking the server to echo them back to you) to know which to try to override.

Upvotes: 1

Related Questions