【问题标题】:BadArgument and JSON parsing error when making an API call进行 API 调用时出现 BadArgument 和 JSON 解析错误
【发布时间】:2018-09-24 20:01:32
【问题描述】:

在我的组件中,我正在尝试使用 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 对象。

下面是我的 fetch 调用:

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.", }

对此有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    改变你的

        body: JSON.stringify(sourceImageUrl),
    

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

    使帖子的正文成为适当的 json 对象。

    【讨论】:

    • 谢谢!!!有效!!!但现在我有另一个问题:expo 相机创建的照片文件返回错误。 Object { "error": Object { "code": "InvalidURL", "message": "Invalid image URL.", } 世博会相机会创建这个:"uri": "file:///var/mobile/Containers/Data/Application/1E1E919E-6C21-4CC7-B9C2-5B4B3BC84B0F/Library/Caches/ExponentExperienceData/%2540chuks93%252Fihu-main/Camera/E67E8249-2B53-49EA-B51D-42CD98768634.jpg" 但会返回错误,但大多数图像中的任何图像地址都可以正常工作。
    • 我认为您会发现可以使用的图像具有诸如 https:// 之类的网络 URL...但是该 file:/// url 将不起作用... azure 无法加载文件本地到您的计算机。您需要将图像放在可以使用 http:// 网址提供服务的地方。我还没有查看 API,但也许有一种方法可以将图像内联为有效负载的一部分,而不仅仅是一个 url。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多