Posting using $fetch

Hey all! I have the following code in a server api endpoint:
const data = await $fetch(`https://api.server.com/logout/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify({
"application_id": `${APPLICATION_ID}`,
"access_token": `${access_token}`
})
})
const data = await $fetch(`https://api.server.com/logout/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify({
"application_id": `${APPLICATION_ID}`,
"access_token": `${access_token}`
})
})
https://api.server.com/logout/ always replays with
error: {
field: 'application_id',
message: 'APPLICATION_ID_NOT_SPECIFIED',
code: 402,
value: null
}
error: {
field: 'application_id',
message: 'APPLICATION_ID_NOT_SPECIFIED',
code: 402,
value: null
}
How do I correctly make the POST? Thank you!
7 Replies
Kyllian
Kyllian6mo ago
how do you handle it in your backend?
ParadoxLector
ParadoxLectorOP6mo ago
https://api.server.com/logout/ Is not really my server but a third party service.
Kyllian
Kyllian6mo ago
have you tried doing a similair thing with postman? just to make sure you're sending the data how they want it to
ʏᴜᴢᴢᴜᴡᴜᴀ
Try using params instead of body, otherwise the suggestion above
ParadoxLector
ParadoxLectorOP6mo ago
I have tried using Insomia and it works like this: POST to URL, structured as Form URL encoded. How do I translate it to my code?
const data = await $fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: {
"application_id": APPLICATION_ID,
"access_token": access_token,
}
})
const data = await $fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: {
"application_id": APPLICATION_ID,
"access_token": access_token,
}
})
This still doesn't work...
IcyFoxer
IcyFoxer6mo ago
Maybe try to add an additional headers "Accept" : "application/json"
ParadoxLector
ParadoxLectorOP6mo ago
This is how it worked in the end:
const data = await $fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
"application_id": APPLICATION_ID,
"access_token": access_token,
})
})
const data = await $fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
"application_id": APPLICATION_ID,
"access_token": access_token,
})
})
Want results from more Discord servers?
Add your server