17

I'm using JQuery to issue an AJAX request to my own Webservice. I need to set or modify the User-Agent HTTP-Header for the HTTP-AJAX-Request, how can I do this the easiest way?

I tried the hint provided by some users to use the setRequestHeader Method to set the User-Agent, but this does not work. It does in fact work for other newly created headers (like X-Test-Header) but it does not work for User-Agent.

theomega
  • 31,591
  • 21
  • 89
  • 127

5 Answers5

28

It is simply impossible, you are not allowed to change the user-agent for XMLHttpRequests. I'm not sure if this is valid for Internet-Explorer, but the w3c specifies here:

The setRequestHeader() method

[...]

When the setRequestHeader(header, value) method is invoked, the user agent must run these steps: [...]

Terminate these steps if header is a case-insensitive match for one of the following headers:

[...]

  • User-Agent
theomega
  • 31,591
  • 21
  • 89
  • 127
  • 10
    2017 update: the W3C TR spec you linked has moved to [this whatwg spec](https://xhr.spec.whatwg.org/#the-setrequestheader()-method), which refers to [this list of forbidden header names in the fetch spec](https://fetch.spec.whatwg.org/#forbidden-header-name), which doesn't include `User-Agent`. MDN [explicitly confirms](https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name) that `User-Agent` is no longer forbidden. Chrome still refuses it, though, so I don't think your advice is invalid quite yet. – mrec Feb 22 '17 at 19:00
15

If you are using jQuery, set the request header in the ajaxSetup.

$.ajaxSetup({
  beforeSend: function(request) {
    request.setRequestHeader("User-Agent","InsertUserAgentStringHere");
  }
});
Peter Olson
  • 139,199
  • 49
  • 202
  • 242
  • Have you tried this? It does not work using JQuery 1.5.2 and Firefox 4, the user-agent is still the default one. I call the ajaxSetup after the document has loaded completely (document.ready) before issuing the calls. It is a simple json-request (not jsonp) – theomega Apr 24 '11 at 19:41
  • This does not work in Chrome with a Mac. I would say in general this doesn't work at all if it's "simply impossible". – Chris Aug 15 '12 at 21:18
  • 8
    Works for me inside my node.js application where I can care less about w3c's rules.. You're awesome - thanks. – Matej Jan 23 '13 at 23:52
  • 1
    Modifying the header does not work at all, however this code works if you are trying to add a new header. – QuakeCore Jul 28 '16 at 07:42
  • It seems the code works but Chrome doesn't want to set it. Refused to set unsafe header "User-Agent" – Svetoslav Marinov Jul 25 '22 at 12:31
6

Well, you could always use jQuery to post to a server-side page which then in turn is able to modify the User-Agent header, and also make a request to the page which would have been directly accessed by jQuery.

Lucas
  • 16,930
  • 31
  • 110
  • 182
2

A way to do this is to overwrite the native code in the __defineGetter__ method of the window.navigator object.

Check this out:

window.navigator.__defineGetter__('userAgent', function () {
    return "___I'M A GHOST___";
});

Run navigator.userAgent in the console before and after to check.

Works in Chrome. I haven't checked other browsers.

You can read more about it here: https://www.codeproject.com/Tips/1036762/Mocking-userAgent-with-JavaScript

riot
  • 307
  • 3
  • 7
  • Anyone seeing this: please do not use this solution (it's not the solution's fault, but it is deprecated): "Warning: This feature is deprecated in favor of defining getters using the object initializer syntax or the Object.defineProperty() API. While this feature is widely implemented, it is only described in the ECMAScript specification because of legacy usage. This method should not be used since better alternatives exist." (see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineGetter__)). – Oleg Valter is with Ukraine Jun 12 '21 at 19:33
0

I have done it like this: I send the ajax request to a seperate php script that acts just like a forwarder, in this php script you will then make the original request and change the user-agent as you want.

You can set the user-agent in file_get_contents like this: https://gist.github.com/vyspiansky/82f4b1ef6fcff160047d

Benni
  • 47
  • 8