进行API调用时BadArgument和JSON解析错误

时间:2018-04-14 17:34:51

标签: ajax api azure react-native fetch-api

在我的组件中,我尝试使用fetch进行api调用。

API采用图像.jpg文件路径 - file:///var/mobile/Containers/Data/Application/1E1E919E-6C21-4CC7-B9C2-5B4B3BC84B0F/Library/Caches/ExponentExperienceData/%2540chuks93%252Fihu-main/Camera/F3B8EBCC-BB09-4603-AF7E-FD3CA792C237.jpg,它应返回JSON个对象。

以下是我的提取电话:

export default  {

processImage: (image) => {
// Replace the subscriptionKey string value with your valid subscription key.
var subscriptionKey = "*******************";

var uriBase = "https://westus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise";

// Display the image.
    var sourceImageUrl = image;
console.log(typeof sourceImageUrl)
console.log("here", sourceImageUrl);

// Perform the REST API call.
return fetch(uriBase, {

        method: 'POST',
    headers: {
        "Content-type": "application/json",
        "Ocp-Apim-Subscription-Key": subscriptionKey
    },

    body: JSON.stringify(sourceImageUrl),
    })
    .then((data) => data.json())
    .then(function (data){
        console.log("hello", data);
    })

    .catch(function (error) {
        console.log(error);
    });

  }
}

当我运行上面的代码时,这是它返回的错误:

Object { "error": Object { "code": "BadArgument", "message": "JSON parsing error.", }

对此有何想法?

1 个答案:

答案 0 :(得分:0)

更改您的

    body: JSON.stringify(sourceImageUrl),

    body: JSON.stringify({url: sourceImageUrl}),

使帖子的主体成为合适的json对象。