首页 文章

从appcelerator调用google Cloud 视觉API时,为什么会收到无效的JSON有效负载?

提问于
浏览
1

我试图使用Alloy Appcelerator的Google vision API v1

我创建一个请求HTTPClient并调用API https://vision.googleapis.com/v1/images:annotate?key=MY_APP_KEY

但我得到谷歌的响应文本:

{
 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请求

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));

谢谢你的帮助

1 回答

  • 5

    通过设置 Content-LengthContent-Type 标头,它应该工作:

    xhr.setRequestHeader("Content-Length", size);
    xhr.setRequestHeader("Content-Type", "application/json");
    

    另外请注意,Google建议将图片大小调整为1024 x 768 - 您可以使用以下方法调整图片大小:

    img = img.imageAsResized(1024,768);
    

    在对我的代码进行这些更改后,我完成了所有工作 .

相关问题