应以何种格式创建获取 POST 请求以访问 Imagga API?

In what format should a fetch POST request be created to access the Imagga API?

我正在使用 React-Native 构建一个向用户推荐服装的移动应用程序。我正在使用 Imagga's API to get the colors of the clothing while excluding the background. I have tried to make a POST request using fetch from analyzing the node.js code given in the documentation:

image_file_b64 = "" + image_file_b64

//Extracting the colors from the object
  let response = await fetch('https://api.imagga.com/v2/colors', {  
    method: 'POST',
    headers: {
        'apiKey': '<PLACEHOLDER>',
        'apiSecret': '<PLACEHOLDER>',
        'Authorization': '<PLACEHOLDER>',
        Accept: 'application/json',
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        image_base64: image_file_b64,
        extract_overall_colors: 0,
    })
})
let responseJson = await response.json()
console.log(responseJson)

但是,我收到的唯一输出是(最后一行记录的内容):

Object {
  "status": Object {
    "text": "Please provide content for processing.",
    "type": "error",
  },
}

我和 Imagga 的人一起解决了这个问题,但他不熟悉 React Native。他建议将内容类型更改为 "application/x-www-form-urlencoded" 或 "application/x-www-form-urlencoded;charset=UTF-8",但都没有用。

我相当确信问题出在我设置提取的方式上。如果有人熟悉 Imagga API,您能否确定代码中的错误或 Imagga 期望的格式与我提供的格式不匹配导致它认为我已经给它输入.谢谢!

fetch body 不正确,你使用的Content-Type 是JSON,为什么你发送字符串。修改如下,试试看

let response = await fetch('https://api.imagga.com/v2/colors', {  
    method: 'POST',
    headers: {
        'apiKey': '<PLACEHOLDER>',
        'apiSecret': '<PLACEHOLDER>',
        'Authorization': '<PLACEHOLDER>',
        Accept: 'application/json',
        'Content-Type': 'application/json',
    },
    body: {
        image_base64: image_file_b64,
        extract_overall_colors: 0,
    })
})

我看了官方的API,里面给出了node.js的例子。你可以根据它进行修改。如果上面代码不成功,可以把content-type改成formdata

let params = {
        image_base64: image_file_b64,
        extract_overall_colors: 0,
    };
let formData = new FromData()
formdata.append('RequestData',JSON.stringify(params))
let response = await fetch('https://api.imagga.com/v2/colors', {  
    method: 'POST',
    headers: {
        'apiKey': '<PLACEHOLDER>',
        'apiSecret': '<PLACEHOLDER>',
        'Authorization': '<PLACEHOLDER>',
         Accept: 'application/json',
        'Content-Type': 'multipart/form-data',
    },
    body: formData)
})

这是官方的api,可以使用postman软件测试请求