N
Nuxt3mo ago
vinayak

$fetch not working


try {
const url = `${BASE_URL}/SSORESTNEW/TokenDetail`;
const options = {
method: 'GET',
headers: {
'Authorization': `Basic ${Buffer.from(`${ssoDefaultUsername}:${ssoDefaultPassword}`).toString('base64')}`,
'content-type': 'application/json',
'SSO-TOKEN': token,
},
};
const response = await fetch(url, options);
const data: TokenDetailResponse = await response.json();
console.log('[getTokenDetail] Response:', data);
return data;
} catch (error) {
console.error('Error fetching token details:', error);
throw error;
}

try {
const url = `${BASE_URL}/SSORESTNEW/TokenDetail`;
const options = {
method: 'GET',
headers: {
'Authorization': `Basic ${Buffer.from(`${ssoDefaultUsername}:${ssoDefaultPassword}`).toString('base64')}`,
'content-type': 'application/json',
'SSO-TOKEN': token,
},
};
const response = await fetch(url, options);
const data: TokenDetailResponse = await response.json();
console.log('[getTokenDetail] Response:', data);
return data;
} catch (error) {
console.error('Error fetching token details:', error);
throw error;
}
The above code works but the below one doesn't in server/api/token routes?
try {
const data = await $fetch<TokenDetailResponse>(`${BASE_URL}/SSORESTNEW/TokenDetail`, {
method: 'GET',
headers: {
'Authorization': `Basic ${Buffer.from(`${ssoDefaultUsername}:${ssoDefaultPassword}`).toString('base64')}`,
'content-type': 'application/json',
'SSO-TOKEN': token,
},
});
return data;
} catch (error) {
console.error('Error fetching token details:', error);
throw error;
}
try {
const data = await $fetch<TokenDetailResponse>(`${BASE_URL}/SSORESTNEW/TokenDetail`, {
method: 'GET',
headers: {
'Authorization': `Basic ${Buffer.from(`${ssoDefaultUsername}:${ssoDefaultPassword}`).toString('base64')}`,
'content-type': 'application/json',
'SSO-TOKEN': token,
},
});
return data;
} catch (error) {
console.error('Error fetching token details:', error);
throw error;
}
I even tried
'Authorization': `Basic ${btoa(`${ssoDefaultUsername}:${ssoDefaultPassword}`)}`,
'Authorization': `Basic ${btoa(`${ssoDefaultUsername}:${ssoDefaultPassword}`)}`,
but no avail.
2 Replies
Alan
Alan3mo ago
You may try npm install --save buffer
import { Buffer } from 'buffer';

// @ts-ignore
window.Buffer = Buffer;
import { Buffer } from 'buffer';

// @ts-ignore
window.Buffer = Buffer;
vinayak
vinayakOP3mo ago
That’s not it since using Buffer works as expected with native fetch but doesn’t without $fetch
Want results from more Discord servers?
Add your server