This works in Chrome (open inspector and paste):
async function main() {
const response = await fetch(
'https://generativelanguage.googleapis.com/v1beta/openai/chat/completions',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ABC123`,
},
body: JSON.stringify({
model: 'gemini-1.5-flash',
messages: [{ role: 'user', content: 'Explain to me how AI works' }],
stream: true,
}),
}
)
console.log('>>>', response.statusText, await response.text())
}
main()
The exact same code does not work in Node. The exact error is:
Bad Request [{
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"status": "INVALID_ARGUMENT"
}
}
Does anyone know what is going on?
For some reason it always fails for Node version of fetch up-to the latest (23).