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 unsafe header "User-Agent"

See original GitHub issue

sample code

const api = new WooCommerceRestApi({
  url: "http://my-personal-store.com",
  consumerKey: "ck_xxxxxxxxxx",
  consumerSecret: "cs_xxxxxxxx",
  version: "wc/v3"
});

const { data : categories } = await api.get(`products/categories`)

fires successfully and return the response but it also spit errors in Chrome console

Refused to set unsafe header “User-Agent”

It is also taking a lot of time to fetch (2-3s) due to throwing and catching (i guess)

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:20 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
Cornettoocommented, Jul 13, 2021

I simply fixed it by adding an empty header object to axiosConifg.

const WooCommerce = new WooCommerceRestApi({
	url: '',
	consumerKey: '',
	consumerSecret: '',
	version: 'wc/v3',
	axiosConfig: {
	  headers: {}
	}
}),
7reactions
OArnarssoncommented, Jun 3, 2020

Having the same issue, the error is coming from the axios setting which is setting the user agent: "User-Agent": "WooCommerce REST API - JS Client/" + this.classVersion Removing it fixes the issue, but obviously this is hacking source code. Is there some way to get this working by over riding it using the axiosConfig option available to WooCommerceRestApi ?

axiosConfig: { headers: {} }, seems to do the trick for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ajax request: Refused to set unsafe header - Stack Overflow
Therefore I need to post a request to their endpoint with the Referer and the User-Agent properly set. This call should return an...
Read more >
Refused to set unsafe header "User-Agent" · Issue #982 - GitHub
Description Every time an api call is made i get the error: Refused to set unsafe header "User-Agent" I'm using the API package...
Read more >
Refused to get unsafe header - TrackJS
The message describes a failed attempt to access header data from a network request. We discovered this error while working on a prototype...
Read more >
Refused to set unsafe header "User-Agent": connection.js
The XMLHttpRequest#setRequestHeader specifically states that User-Agent must not be set by a client script, and any attempt to set that ...
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 >

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