Reputation: 11
I have a java application that authenticate users using Http header (HttpServletRequest.getHeader('html-wg-useruid')), How can i set this header from javascript ?
Upvotes: 1
Views: 2121
Reputation: 1554
You can use the setRequestHeader method from XMLHttpRequest
.
var httprequest = new XMLHttpRequest();
httprequest.setRequestHeader('Accept', 'application/json');
httprequest.setRequestHeader('Content-Type', 'application/json');
httprequest.setRequestHeader('Accept-Language', 'en-us');
To retrieve a specific header from the response when you need them, use the getResponseHeader method. To get all of them, use getAllResponseHeaders.
var contentType = httprequest.getResponseHeader('Content-Type');
var responseHeaders = httprequest.getAllResponseHeaders();
A full list of the available methods can be found in MDN's XMLHttpRequest page. Take a look at it if you are interested.
Upvotes: 1