question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refused to set 'user-agent'

See original GitHub issue

This isn’t technically an issue with this library, however it should be noted that neither Chrome nor Safari currently allow setting the “user-agent” header, both throw Refused to set unsafe header "user-agent" errors.

It might be responsible to detect whether or not the browser allows for setting this header to avoid spurious errors getting thrown.

I’m happy to open a PR if there’s a chance it will go in. We’d certainly like that, as currently we’re getting this error on every page load.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:12
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
jverhoelencommented, Jan 16, 2020

In Chrome for example the annoying issue of logging the errors Refused to set unsafe header "user-agent" and Refused to set unsafe header "Accept-Encoding" still persists. Both headers are present in the list of automatically set headers but xhr.js adapter is not filtering them out.

I think the block in xhr.js handling this should look like this:

// Add headers to the request
if ('setRequestHeader' in request) {
  utils.forEach(requestHeaders, function setRequestHeader(val, key) {
    if (typeof requestData === 'undefined' && key.toLowerCase() === 'content-type') {
      // Remove Content-Type if data is undefined
      delete requestHeaders[key];
    } else if (key.toLowerCase() === 'user-agent' || key.toLowerCase() === 'accept-encoding') {
        // don't do anything
    } else {
      // Otherwise add header to the request
        console.log('axios/lib/adapters/xhr')
      request.setRequestHeader(key, val);
    }
  });
}

Is there any reason against solving it like this which should be considered?

2reactions
MarkPieszakcommented, Jun 14, 2021

In Chrome for example the annoying issue of logging the errors Refused to set unsafe header "user-agent" and Refused to set unsafe header "Accept-Encoding" still persists. Both headers are present in the list of automatically set headers but xhr.js adapter is not filtering them out.

I think the block in xhr.js handling this should look like this:

// Add headers to the request
if ('setRequestHeader' in request) {
  utils.forEach(requestHeaders, function setRequestHeader(val, key) {
    if (typeof requestData === 'undefined' && key.toLowerCase() === 'content-type') {
      // Remove Content-Type if data is undefined
      delete requestHeaders[key];
    } else if (key.toLowerCase() === 'user-agent' || key.toLowerCase() === 'accept-encoding') {
        // don't do anything
    } else {
      // Otherwise add header to the request
        console.log('axios/lib/adapters/xhr')
      request.setRequestHeader(key, val);
    }
  });
}

Is there any reason against solving it like this which should be considered?

I have ran into this exact issue as well (not using Axios directly, but something else in an Application is). A fix like above does infact make it go away, but that’s only temporarily + locally (aside from writing scripts to update this after “install” 😄)

If it’s as simple as getting a PR in to fix this, I’d be more than happy to help! (Unless there’s a good reason we can’t do this?)

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to resolve Refused to set unsafe header "User-Agent" in ...
I can run similar code from C# in the browser without any issues, but not in Javascript. Is there anyway around this. ......
Read more >
Refused to set unsafe header "User-Agent" Error with Dall-E ...
Trying the Dall-E api, give the Refused to set unsafe header “User-Agent” Error. Why would Open AI set this so strict, to only...
Read more >
Refused to set an unsafe header "User-Agent" - Trailhead
Hi,. When im clicking on checkbox in Vf page the following error is occured. what s the meaning of this error Refused to...
Read more >
BrowserClient sets unsafe headers causing Chrome to report ...
However on each such send request, Chrome reports an error saying: Refused to set unsafe header "user-agent" Refused to set unsafe header "content-length"....
Read more >
Browser Error: "Refused to set unsafe header 'User Agent'"
I'm not sure it's related, but I noticed this on twitter: .@salesforce users please update your Google Chrome to 31.0.1650.57.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found