我应该在 google translate api 请求正文中输入什么参数?
What argument should i put in google translate api request body?
try {
const response = await fetch(googleTranslateApi + apiKey, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
charset: 'UTF-8',
},
body: JSON.stringify({
requests: [
{
q: 'Hello My Friend',
target: 'zh',
}
]
})
});
const responseJson = await response.json();
我正在尝试调用 google 翻译 API,但我一直收到此错误:
error: {code: 400, message: "Missing required field target", errors: Array(1), status: "INVALID_ARGUMENT"}
我在请求正文中遗漏了什么?
我相信您的请求正文中的数据正确,只是格式不正确。您将 q
和 target
属性嵌套在一个对象内,该对象位于您的顶级请求正文对象内的数组内。相反,将 q
和 target
属性直接放在顶级请求正文对象中,如下所示:
try {
const response = await fetch(googleTranslateApi + apiKey, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
charset: 'UTF-8',
},
body: JSON.stringify({
q: 'Hello My Friend',
target: 'zh'
})
});
const responseJson = await response.json();
try {
const response = await fetch(googleTranslateApi + apiKey, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
charset: 'UTF-8',
},
body: JSON.stringify({
requests: [
{
q: 'Hello My Friend',
target: 'zh',
}
]
})
});
const responseJson = await response.json();
我正在尝试调用 google 翻译 API,但我一直收到此错误:
error: {code: 400, message: "Missing required field target", errors: Array(1), status: "INVALID_ARGUMENT"}
我在请求正文中遗漏了什么?
我相信您的请求正文中的数据正确,只是格式不正确。您将 q
和 target
属性嵌套在一个对象内,该对象位于您的顶级请求正文对象内的数组内。相反,将 q
和 target
属性直接放在顶级请求正文对象中,如下所示:
try {
const response = await fetch(googleTranslateApi + apiKey, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
charset: 'UTF-8',
},
body: JSON.stringify({
q: 'Hello My Friend',
target: 'zh'
})
});
const responseJson = await response.json();