Next.js Discord

Discord Forum

Cross-Origin Request Blocked. CORS header ‘Access-Control-Allow-Origin’ missing when fetching

Answered
MD20M posted this in #help-forum
Open in Discord
So I wanted to delete a specific role from a discord user using fetch in my next.js app. But I encountered this error
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https:// discord.com/api/v10/guilds/XXXX/members/XXXX/roles/XXXXX. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 403

My code is:
const response = await fetch(`https://discord.com/api/v10/guilds/${guild}/members/${userId}/roles/${oldRoleId}`, {
      method: "DELETE",
      headers: {
        Authorization: `Bot ${botToken}`,
      },
    });
  
    if (!response.ok) {
      throw new Error(`Failed to remove role: ${await response.text()}`);
    }

I have tried adding the header in the error but I just can't figure out how to do it correctly. Thank you in advance!
Answered by Clown
You can try using a server action or a route handler if you want to use it in a client component
View full answer

20 Replies

Im not master at CORs. Take my words with a grain of salt.

Adding the header to the request will not work. Are you running this fetch in a server component? Also if its inside a client component then i would say you should move it into a route handler
You can try using a server action or a route handler if you want to use it in a client component
Answer
(And you cant pass it in for some reason or don't prefer doing that).
@Clown You can try using a server action or a route handler if you want to use it in a client component
Thank you that worked! I have no idea how I didn't think of that
@MD20M Thank you that worked! I have no idea how I didn't think of that
Basset Bleu de Gascogne
could you tell me what you did please
@Basset Bleu de Gascogne could you tell me what you did please
You just have to place the code which does the request in a server component
@MD20M You just have to place the code which does the request in a server component
Basset Bleu de Gascogne
But I want to access it in the client side so I can show it in the ui
@MD20M You just have to place the code which does the request in a server component
Basset Bleu de Gascogne
I will tag you in the forum I just created for it
Red-cockaded Woodpecker
if you have a back end, install the cors package, npm i cors
@Red-cockaded Woodpecker if you have a back end, install the cors package, npm i cors
Basset Bleu de Gascogne
nope front end
Red-cockaded Woodpecker
your problem comes from the requests you make, but they block them for security reasons
His problem is just that you can't make a request in a client component
For anyone in the future who might need it: #having cors error while fetching from client side
Red-cockaded Woodpecker
do you want to make a discord bot without using a back end???
And neither was I with this forum
Please make another post if its related to code help and nextjs