CORS error on Users API

I’m sure I’m doing something wrong here, but I don’t know what.

I’m trying to make a request to the Users API however I keep getting a CORS error.

Code:

const req = await fetch('https://users.roblox.com/v1/usernames/users/', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    "usernames": [
      robloxUsername
    ],
    "excludeBannedUsers": true
  }),
})

const { data: { users } } = await req.json();
const id = users[0].id;

Is there something I’m missing or is my browser just not liking me today?

Thanks!

Edit: CORS error in question is: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://users.roblox.com/v1/usernames/users/. (Reason: CORS request did not succeed). Status code: (null)

1 Like

You have to use a proxy, you can’t make requests directly to roblox.com from a game

Edit: sorry, misread what you are doing, going to write a different reply

1 Like

This isn’t from a game, it’s from my browser using Javascript’s fetch() function.

As the error you’re getting is complaining about cors, try to include the mode parameter in the request:

const req = await fetch('https://users.roblox.com/v1/usernames/users/', {
  method: 'POST',
  mode: 'no-cors', --this is what i added
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    "usernames": [
      robloxUsername
    ],
    "excludeBannedUsers": true
  }),
})

const { data: { users } } = await req.json();
const id = users[0].id;

Further I suppose this is up to personal preference, but why don’t you use:

fetch().then(() => {})

That won’t work sadly, already tried. It force sets the content type to text/plain instead of application/json like it needs to be.

Have you found a solution yet? I’m running into the same error, and the annoying part is, only Roblox’s apis are doing it. I have no problem using other non roblox apis.

Hi, AmusedCrepe!

I encountered this issue yesterday. What I did as a solution was I made a server (using express) and just basically made a proxy which would do the requests on the express server and respond back with the results and made sure to set Access-Control-Allow-Origin to * in the response headers. (Example below.)


(Note in order to use fetch in node.js you will have to install a third party package, node-fetch)

This is a good resource to help you understand CORS. Understanding CORS. If you ever worked with an AJAX call… | by Bartosz Szczeciński | Medium

I hope this helps!

2 Likes