【发布时间】:2020-06-22 14:50:51
【问题描述】:
我正在尝试实现微软翻译 API。 (https://rapidapi.com/microsoft-azure-org-microsoft-cognitive-services/api/microsoft-translator-text)
我想将它与对话框流集成,而对话框流不适用于 axios 函数,所以我尝试使用 fetch 使用异步函数。
这段代码运行良好
axios({
"method": "POST",
"url": "https://microsoft-translator-text.p.rapidapi.com/translate",
"headers": {
"content-type": "application/json",
"x-rapidapi-host": "microsoft-translator-text.p.rapidapi.com",
"x-rapidapi-key": "key",
"accept": "application/json",
"useQueryString": true
}, "params": {
"profanityAction": "NoAction",
"textType": "plain",
"to": "hi",
"api-version": "3.0"
}, "data": [{
"Text": "Hello, World"
}]
})
.then((response) => {
console.log(response);
console.log(response.data[0].detectedLanguage.language);
console.log(response.data[0].translations[0].text);
})
.catch((error) => {
console.log(error);
})
但是当我使用 fetch 使用相同的代码时,它会给出错误
async function translate() {
const response = await fetch(`https://microsoft-translator-text.p.rapidapi.com/translate`, {
"method": "POST",
"headers": {
"content-type": "application/json",
"x-rapidapi-host": "microsoft-translator-text.p.rapidapi.com",
"x-rapidapi-key": "key",
"accept": "application/json",
"useQueryString": true
}, "params": {
"profanityAction": "NoAction",
"textType": "plain",
"to": "hi",
"api-version": "3.0"
}, "data": [{
"Text": "Hello, World"
}]
});
const jsonres = await response.json();
console.log(jsonres);
}
translate();
这是错误
error:
code: 400000
message: "One of the request inputs is not valid."
【问题讨论】:
-
因为 fetch 使用不同的参数格式。请阅读:developer.mozilla.org/en-US/docs/Web/API/Fetch_API
-
您的问题解决了吗?如果您需要进一步的帮助,请告诉我。
-
是的,非常感谢,问题已解决。
标签: javascript node.js azure axios fetch