应该以哪种格式创建访存POST请求以访问Imagga API?

我正在使用React-Native构建一个向用户推荐服装的移动应用程序。我正在使用Imagga's API来获取衣服的颜色,同时不包括背景。我已经尝试通过分析documentation中给出的node.js代码来使用fetch发出POST请求:

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期望的内容与我提供的内容之间的格式不匹配,从而导致它不认为我已输入了它。谢谢!

czzzb 回答:应该以哪种格式创建访存POST请求以访问Imagga API?

获取主体不正确,您使用的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示例。您可以根据它进行修改。如果上面的代码不成功,则可以将内容类型更改为formdata

let params = {
        image_base64: image_file_b64,};
let formData = new FromData()
formdata.append('RequestData',JSON.stringify(params))
let response = await fetch('https://api.imagga.com/v2/colors','Content-Type': 'multipart/form-data',body: formData)
})

这是官方api,您可以使用邮递员软件测试请求

本文链接:https://www.f2er.com/3168311.html

大家都在问