Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS/jQuery get HTTPRequest request headers?

Tags:

Using getAllResponseHeaders in the xhr object, is possible to get all the response headers after an ajax call. But I can't found a way to get the Request headers string, is that possible ?

like image 513
Lwyrn Avatar asked May 09 '12 12:05

Lwyrn


People also ask

How do I get a header from ajax request?

ajaxSetup({ headers: { 'custom-header': 'some value' } }); // Sends your custom header $. ajax({ url: 'edureka/co' }); // Overwrites the default header with a new header $. ajax({ url: 'edureka/co', headers: { 'some-other-header': 'some value' } }); To add a header to every request then use the beforeSend hook with $.

How do I find a header value?

get() The get() method of the Headers interface returns a byte string of all the values of a header within a Headers object with a given name. If the requested header doesn't exist in the Headers object, it returns null .

How do I see curl request?

We can use curl -v or curl -verbose to display the request headers and response headers in the cURL command. The > lines are request headers . The < lines are response headers .

How do I pass headers in XMLHttpRequest?

The XMLHttpRequest method setRequestHeader() sets the value of an HTTP request header. When using setRequestHeader() , you must call it after calling open() , but before calling send() . If this method is called several times with the same header, the values are merged into one single request header.


2 Answers

If this is for debugging purposes then you can just use Firebug or Chrome Developer Tools (and whatever the feature is called in IE) to examine the network traffic from your browser to the server.

An alternative would be to use something like this script:

$.ajax({     url: 'someurl',     headers:{'foo':'bar'},     complete: function() {         alert(this.headers.foo);     } }); 

However I think only the headers already defined in headers is available (not sure what happens if the headers are altered (for instance in beforeSend).

You could read a bit more about jQuery ajax at: http://api.jquery.com/jQuery.ajax/

EDIT: If you want to just catch the headers on all calls to setRequestHeader on the XMLHttpRequest then you can just wrapp that method. It's a bit of a hack and of course you would need to ensure that the functions wrapping code below is run before any of the requests take place.

// Reasign the existing setRequestHeader function to  // something else on the XMLHtttpRequest class XMLHttpRequest.prototype.wrappedSetRequestHeader =    XMLHttpRequest.prototype.setRequestHeader;   // Override the existing setRequestHeader function so that it stores the headers XMLHttpRequest.prototype.setRequestHeader = function(header, value) {     // Call the wrappedSetRequestHeader function first      // so we get exceptions if we are in an erronous state etc.     this.wrappedSetRequestHeader(header, value);      // Create a headers map if it does not exist     if(!this.headers) {         this.headers = {};     }      // Create a list for the header that if it does not exist     if(!this.headers[header]) {         this.headers[header] = [];     }      // Add the value to the header     this.headers[header].push(value); } 

Now, once the headers have been set on an XMLHttpRequest instance we can get them out by examining xhr.headers e.g.

var xhr = new XMLHttpRequest(); xhr.open('get', 'demo.cgi'); xhr.setRequestHeader('foo','bar'); alert(xhr.headers['foo'][0]); // gives an alert with 'bar' 
like image 166
Emil H Avatar answered Dec 04 '22 05:12

Emil H


Something you could to is use Sinon's FakeXMLHttpRequest to replace your browser's XHR. It's described in this document on how to use it for testing but I'm pretty sure you can use the module for your debugging purposes.

What you need to do is:

var requests; this.xhr = sinon.useFakeXMLHttpRequest(); this.xhr.onCreate = function(xhr) {     requests.push(xhr); } 

And then later on, you can check your requests array for headers by:

console.log(requests[0].requestHeaders); 

To access your request headers.

like image 44
Reza S Avatar answered Dec 04 '22 04:12

Reza S