Search code examples
javascriptjsongoogle-apigoogle-cloud-platformgoogle-cloud-vision

Google Vision API


I'm trying to post data to Google Vision API. Butt i always get 400 response code - bad request. I have no ideas already.

I have read and tried to use those links:

https://cloud.google.com/vision/docs/requests-and-responses https://cloud.google.com/vision/docs/detecting-text https://developers.google.com/apis-explorer/?hl=ru#p/vision/v1/vision.images.annotate

And i came up to this:

Here is my data to post:

var dataToSend = {
        "requests": [
           {
               "image": {
                   "content": imageData
               },
               "features": [
                  {
                      "type": "TYPE_UNSPECIFIED",
                      "maxResults": 50
                  },
                  {
                      "type": "LANDMARK_DETECTION",
                      "maxResults": 50
                  },
                  {
                      "type": "FACE_DETECTION",
                      "maxResults": 50
                  },
                  {
                      "type": "LOGO_DETECTION",
                      "maxResults": 50
                  },
                  {
                      "type": "LABEL_DETECTION",
                      "maxResults": 50
                  },
                  {
                      "type": "TEXT_DETECTION",
                      "maxResults": 50
                  },
                  {
                      "type": "SAFE_SEARCH_DETECTION",
                      "maxResults": 50
                  },
                  {
                      "type": "IMAGE_PROPERTIES",
                      "maxResults": 50
                  }
               ]
           }
        ]
    };

And here is my post:

$.ajax({
        url: "https://vision.googleapis.com/v1/images:annotate?fields=responses&key={MY CREATED KEY}",
        type: "POST",
        data: dataToSend,
        success: function (reponse) {
            console.log(reponse);
        },
    });

Here is data from console (THE CONTENT IS BLANK FOR EXAMPLE ( not to post wole base64 )):

enter image description here

And here is the response:

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"requests[0][features][0][type]\": Cannot bind query parameter. Field 'requests[0][features][0][type]' could not be found in request message.\nInvalid JSON payload received. Unknown name \"requests[0][features][0][maxResults]\": Cannot bind query parameter. Field 'requests[0][features][0][maxResults]' could not be found in request message.\nInvalid JSON payload received. Unknown name \"requests[0][image][content]\": Cannot bind query parameter. Field 'requests[0][image][content]' could not be found in request message.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"requests[0][features][0][type]\": Cannot bind query parameter. Field 'requests[0][features][0][type]' could not be found in request message."
          },
          {
            "description": "Invalid JSON payload received. Unknown name \"requests[0][features][0][maxResults]\": Cannot bind query parameter. Field 'requests[0][features][0][maxResults]' could not be found in request message."
          },
          {
            "description": "Invalid JSON payload received. Unknown name \"requests[0][image][content]\": Cannot bind query parameter. Field 'requests[0][image][content]' could not be found in request message."
          }
        ]
      }
    ]
  }
}

Where is my mistake here?


Solution

  • So i found what was the issue. My photo exceeded the data size limit that is about 4MB