将curl请求转换为获取api watson

时间:2018-07-05 16:04:24

标签: javascript reactjs curl fetch fetch-api

我希望将以下curl请求转换为访存api。

curl -X POST -u "apikey:{your_api_key}" --form "images_file=@fruitbowl.jpg" \ "https://gateway.watsonplatform.net/visual-recognition/api/v3/classify?version=2018-03-19"

我得到err TypeError {}

这是我的代码:

const body = new FormData();
    body.append("images_file", image); //event.target.files[0]
    body.append("apikey", 'apikey');

    fetch("https://gateway.watsonplatform.net/visual-recognition/api/v3/classify?version=2018-03-19", {
      body,
      headers: {
        "Content-Type": "multipart/form-data"
      },
      method: "POST"
    }).then(response => {
      console.log(response);
    }).then(data => {
      console.log(data);
    }).catch(err => {
      console.log('err', err);
    })

从我的html表单输入中,我直接使用event.target.files[0] which is image.

设置文件对象

0 个答案:

没有答案