Why do I get invalid JSON payload when calling google cloud vision API from appcelerator?

爷,独闯天下 提交于 2019-12-24 12:48:34

问题


I was trying to use Google vision API v1 by Alloy Appcelerator

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";
  };

}

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

Thanks for your help


回答1:


By setting the Content-Length and Content-Type headers it should work:

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

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.



来源:https://stackoverflow.com/questions/35885801/why-do-i-get-invalid-json-payload-when-calling-google-cloud-vision-api-from-appc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!