I have a single page js app and I’m sending custom headers to my server containing logs, but i need to control the size of those headers because my server won’t accept requests larger then 8k.
My solution thus far was to intercept all outgoing ajax request from my application.
I’m using jQuery Global Ajax Events, particularly ajaxSend to intercept all requests. I cannot use beforeSend because that is a local event.
I can’t seem to access the request headers in the callback. I need to read all request’s header and cut down the logs header if it’s too large.
You want to use
beforeSendto modify the request before it is being sent. This is all covered in the documentation you’ve linked to.The global
ajaxSendevent will not help you tamper with the request. The closest thing to global you can get would be to is callajaxSetup, passing abeforeSendoption to be default for all subsequent ajax calls.There appears to be no simple way of getting request headers from an
XMLHttpRequestobject. Since I assume you’re setting your logging headers yourself, however, you might be able to hook into the setting of these headers, and store an accessible reference to them:In this manner, you should be able to directly inspect the
jqXHR.readableHeadersobject for your specific logging header, inbeforeSend, and callsetRequestHeaderonce more, to truncate the string, if needed.To retrieve the headers you need access to the underlying instance of XMLHttpRequest from jqXHR object. Use
xhr()function to retrieve the instance.