I'm trying to fetch my guild info from discord API
fetch('https://discord.com/api/guilds/772037458996101140', {
headers: {
authorization: 'bot Nzc******zNjYzOTAz.GPwhCV.rhIcH****5R8ZS-cIo4MjPcBVxO6wYsUXhY'
},
})
.then(result => result.json())
.then(response => {
console.log(response);
})
.catch(console.error)
only reciving 401 (unauthorized)
i tried changing "bot" to "BOT", "Bot", "Bearer"
and doing authorization using access token from Oauth2
The problem lies in how the Authorization header has been specified. The correct way specify headers would be:
fetch('https://discord.com/api/guilds/772037458996101140', {
withCredentials: true,
credentials: 'include',
headers: {
'Authorization': 'bot Nzc******zNjYzOTAz.GPwhCV.rhIcH****5R8ZS-cIo4MjPcBVxO6wYsUXhY',
},
})
.then(result => result.json())
.then(response => {
console.log(response);
})
.catch(console.error)
Hope this helps.
Edit
Also added crendentials and withCredentials based on this answer.
I was sending http request from front-end and when i moved it to back-end or just used python test file it worked using : Authorization: 'Bot <TOKEN>'
not sure why but it works how it should mby somebody can explain ?
Related
I have a POST request to the Cloud Functions on Firebase. When I'm trying to make a request, I get a CORS policy error. Ok, I set mode: 'no-cors' and get Failed to load resource: the server responded with a status of 500 ().
Here is the code
let myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Accept", "application/json");
let raw = JSON.stringify({
"description": "Test item",
"email": "testemail#gmail.com"
});
let requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
mode: 'no-cors',
redirect: 'follow',
};
fetch("https://someURl.cloudfunctions.net/someRequest", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
The code is copied from Postman
Any ideas?
As confirmed by jaba, the issue was on server side. Also, same has been documented here.
I'm trying to make members join a server using oauth2.
Whenever I use the api/guilds/${guild}/members/${user} endpoint, I get this error
{ message: '400: Bad Request', code: 0 }
The code I used:
let gid = '868120277077360660';
let usid = '591950387112574988';
fetch(`https://discord.com/api/guilds/${gid}/members/${usid}`, {
method: 'PUT',
access_token: `Bearer ${accessToken}`,
headers: {
authorization: `Bot ${botToken}`,
"Content-Type": "application/json",
},
payload: {
access_token: `Bearer ${accessToken}`,
}
})
.then(result => result.json())
.then(async reo => console.log(reo));
I'm using node-fetch as fetch, in the code. I'm using NodeJS.
I tried many solutions, but none of them worked, it would be great if you can guide me through!
Thank you!
How do we configure the Fetch API to include the API key header?
I've created an API that I can successfully receive responses from POSTMAN or Fiddler by including the API key in the headers.
However, from my code (React / Javavascript) using the following snippet fails;
return fetch(url)
.then(response => response.json(),{
mode: 'cors',
headers: {
'x-api-key': '5485748746547e847483983343433243',
'User-Agent' : 'My-App',
'Accept': '*/*',
},
})
.catch(error => console.log('Error while fetching:', error))
In Postman I can remove all the headers except the x-api-key and it works fine. No combination of headers or configuration seems to work in my code.
If I capture the request in Fiddler, the x-api-key header has not been added by the Fetch request.
What is the correct way to configure fetch to send the api key header?
Your options are in the wrong place. They should be in the 2nd parameter of the fetch function.
return fetch(url, {
mode: 'cors',
headers: {
'x-api-key': '5485748746547e847483983343433243',
'User-Agent' : 'My-App',
'Accept': '*/*',
},
})
.then(response => response.json())
.catch(error => console.log('Error while fetching:', error))
I'm making a bot that can change url code with a request, but i've tried something, and it's only returning the current invite code
My code:
fetch('https://www.discord.com/api/v6/guilds/762359120031907871/vanity-url', {
method: 'POST',
headers: { 'Authorization': 'Bot ' + client.token, 'Content-Type': 'application/json'},
payload: JSON.stringify({
"code":"test458"
})
})
.then(res => res.json())
.then(json => { console.log(json)})
And it's returning
{ code: 'cherrys', uses: 0 }
I just want to change the code, but idk how i can do that, and when i try with a user token it say
401: Unauthorized
Can someone help me with that?
The prefix Bot on the Autorization header is only for Bots, if you want to use a user token, you need to use Bearer prefix instead of the bot ones.
fetch('https://www.discord.com/api/v6/guilds/762359120031907871/vanity-url', {
method: 'POST',
headers: { 'Authorization': `Bearer ${client.token}`, 'Content-Type': 'application/json'},
payload: JSON.stringify({
"code":"test458"
})
})
.then(res => res.json())
.then(json => { console.log(json)});
Im trying to make a request from my React App to my backend server, the frontend do a OPTIONS request and that is OK, the problem is that my then on the fetch console logs the OPTIONS response, and not the real response that is made efter the OPTIONS request.
fetch('http://localhost:8080/api/kp/ticket', {
headers: {
"token": sessionStorage.getItem('token')
},
mode: 'cors',
method: 'GET'
}).then(data => console.log(data));
try this
fetch('http://localhost:8080/api/kp/ticket', {
headers: {
"token": sessionStorage.getItem('token')
},
mode: 'cors',
method: 'GET'
}).then(data => (data.json())
.then(res => console.log(res));
Check in "Other" tab in chrome devtools. Not in XHR