为什么从 appcelerator 调用谷歌云视觉 API 时会得到无效的 JSON 有效负载? [英] Why do I get invalid JSON payload when calling google cloud vision API from appcelerator?
问题描述
我试图使用 Alloy Appcelerator 的 Google Vision API v1
I was trying to use Google vision API v1 by Alloy Appcelerator
我创建一个请求 HTTPClient 并调用 API https://vision.googleapis.com/v1/images:annotate?key=MY_APP_KEY
I create a request HTTPClient and call API https://vision.googleapis.com/v1/images:annotate?key=MY_APP_KEY
但我收到了来自谷歌的回复文本:
But i have get response text from google :
{
error = {
code = 400;
details = (
{
"@type" = "type.googleapis.com/google.rpc.BadRequest";
fieldViolations = ({
description = "Invalid JSON payload received. Unknown name \"request\": Cannot bind query parameter. Field 'request' could not be found in request message.";
});
}
);
message = "Invalid JSON payload received. Unknown name \"request\": Cannot bind query parameter. Field 'request' could not be found in request message.";
status = "INVALID_ARGUMENT";
};
}
还有我的代码使用 Alloy 的 HTTP 请求
And there is my code use HTTP request by Alloy
var requests =
{
"requests":[
{
"image":{
"content": "image_have_encodebase64",
},
"features":[
{
"type":"TEXT_DETECTION",
"maxResults":1
}
]
}
]
};
var xhr = Titanium.Network.createHTTPClient();
xhr.open("POST", 'https://vision.googleapis.com/v1/images:annotate?key=MY_APP_KEY');
xhr.send(JSON.stringify(requests));
感谢您的帮助
推荐答案
通过设置 Content-Length
和 Content-Type
标题,它应该可以工作:
By setting the Content-Length
and Content-Type
headers it should work:
xhr.setRequestHeader("Content-Length", size);
xhr.setRequestHeader("Content-Type", "application/json");
另外应该注意的是,Google 建议将您的图片大小调整为 1024 x 768 -- 您可以使用以下方法调整您的图片大小:
Also it should be noted that Google recommends resizing your image to 1024 x 768 -- you can resize your image with:
img = img.imageAsResized(1024,768);
在对我的代码进行这些更改后,我一切正常.
After making these changes to my code I had everything working.
这篇关于为什么从 appcelerator 调用谷歌云视觉 API 时会得到无效的 JSON 有效负载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!