【问题标题】:Google Cloud Vision REST API Request ErrorGoogle Cloud Vision REST API 请求错误
【发布时间】:2019-07-03 06:27:52
【问题描述】:

我想使用 Cloud Vision API 来检测标签。使用以下代码通过我的后端文件发出请求。

    import {vision} from 'backend/cloudVision.jsw';

    let url = {
  requests:[
    {
      image:{
        source:{
          imageUri: "https://static.wixstatic.com/media/33aa21_89b30ab70dcb480992ec7b96b316a907~mv2_d_2000_2000_s_2.png"
        }
      },
      features:[
        {
          type:"LABEL_DETECTION",
          maxResults:10
        }
      ]
    }
  ]
};

export function button1_click(event) {
    vision(url)
    .then( (response) => {
        console.log(response);
    });
}

在我的后端文件 cloudVision.jsw 我有如下 POST 请求设置

import {fetch} from 'wix-fetch';

export async function vision(url) {

  const apiKey = "API_KEY_HERE"; 

  const response = await fetch("https://vision.googleapis.com/v1/images:annotate?key=" + apiKey, {
    method: 'post',
    headers: {
      "Content-Type": "application/json"
    },
    body: url
  });

  if (response.status >= 200 && response.status < 300) {
    const ret = await response.json();
      return ret;
  }
    let res = await response.json();
    return res;
}

但是,我在发出 API 请求时收到 400 错误“收到无效的 JSON 有效负载。意外的令牌。\n[object Object]\n ^”。

我认为可能是编码问题,但不确定。

为此使用 Corvid。

【问题讨论】:

    标签: javascript google-cloud-platform google-cloud-vision velo


    【解决方案1】:

    是的,这是请求编码方式的问题。我对科维德不熟悉。也许尝试将键放在引号中,例如:“请求”而不是请求。看起来 Corvid 正在添加“\n”,因此请尝试在一行中定义 url。

    【讨论】:

      猜你喜欢
      • 2019-03-13
      • 2018-08-13
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      • 2023-03-08
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多