有人能发现任何可能解释为什么api客户端给我禁止的错误的问题吗?我知道凭证是正确的,因为GET请求--在url工作查找中--有相同的信息。
提前谢谢你
app.get('/translate', (req, res) => {
var textToTranslate = "Hello friend"
const targetLanguage = "ES"
var link = `https://api-free.deepl.com/v2/translate`
var options =
{
method: 'POST',
headers: {
"Host": 'api-free.deepl.com',
"Content-Length": 54,
"Content-Type": 'application/x-www-form-urlencoded',
"User-Agent": "YourApp",
"Accept": "*/*",
},
body: JSON.stringify({
'auth_key': deeplAccessCode,
'text': textToTranslate,
'target_lang': targetLanguage
}),
}
return fetch(link, options)
.then((response) => {
console.log(response)
return response.json(); //Transform http body to json
})
.then((json)=> {
res.send(json) //return json to browser
})
.catch(e => {
console.log(e)
return res.sendStatus(400);
});
})发布于 2022-02-21 14:08:10
它可能失败了,因为您将身体的Content-Type设置为application/x-www-form-urlencoded (根据DeepL API规范是正确的),但是提供了一个JSON (这需要内容类型为application/json)。
您需要提供一个URL编码的正文,就像您也可以在?之后附加到URL的部分一样。也见这个答案 on SO。
https://stackoverflow.com/questions/68423213
复制相似问题