为什么我对Kairos api的REST请求说请求缺少必需的参数?

时间:2015-07-28 17:58:44

标签: c# rest restsharp

我正在尝试连接到kairos api以熟悉它并使用它们的检测功能。

这个api还没有正式的C#库。有一个似乎没有积极维护。

我正在尝试使用RestSharp直接连接。服务器正在接收我的请求,如果我省略app_id和app密钥进行身份验证,就会响应我的预期。所以那部分(添加标题)似乎正在起作用。

添加参数似乎失败了。根据他们的网站:https://www.kairos.com/docs/face-recognition/,唯一需要的参数是" image"使用url或base64编码的照片。

我添加了参数:

var imageURL = new Uri("http://media.kairos.com/kairos-elizabeth.jpg");

        request.AddParameter("image", "{\"url\":\"" + imageURL + "\"}");

但响应仍然是:"Error code: 1002", "Message": One or more required parameters are missing.

我怀疑uri是如何构建的,但我真的不能把它放在它上面。我复制了语法

        request.AddParameter("image", "{\"url\":\"" + imageURL + "\"}");

来自我前面提到的同一个C#SDK。我也试过它:

var imageURL = new Uri("http://media.kairos.com/kairos-elizabeth.jpg");

        request.AddParameter("image", imageURL);

没有成功。

任何人都可以帮助我吗?

编辑: 为了清楚起见,完整代码如下所示:

static void Main(string[] args)
    {
        var client = new RestClient();
        client.BaseUrl = new Uri("https://api.kairos.com/");

        var request = new RestRequest("detect", Method.POST);
        request.AddHeader("Content-Type", "application/JSON");
        request.AddHeader("app_id", "MY app ID");
        request.AddHeader("app_key", "My app KEY");

        var imageURL = new Uri("http://media.kairos.com/kairos-elizabeth.jpg");

        request.AddParameter("image", "\"url\":\"" + imageURL + "\"");
        request.AddParameter("selector:", "FACE");
        request.AddParameter("minHeadScale:", "0.125");
        IRestResponse response = client.Execute(request);

        Console.WriteLine(response.Content);


    }

1 个答案:

答案 0 :(得分:6)

在发送之前,您必须将参数序列化为JSON。

我创建了一个dotnetfiddle来演示这个: https://dotnetfiddle.net/VrKZo2

文档不正确,您只需传递image字段即可完成所有工作。

namespace ConsoleApplication3
{
    using System;

    using RestSharp;

    public class Program
    {
        public static void Main()
        {
            string appId = "YOUR APP ID HERE";
            string appKey = "YOUR APP KEY HERE";
            var client = new RestClient("https://api.kairos.com");
            var request = new RestRequest("detect", Method.POST);

            // automatically makes the request body serialize as JSON
            request.RequestFormat = DataFormat.Json;
            request.AddBody(new { image = "http://media.kairos.com/kairos-elizabeth.jpg" });
            request.AddHeader("app_id", appId);
            request.AddHeader("app_key", appKey);

            var response = client.Execute(request);

            // handle response however you want, but I'm just going to print it out
            Console.WriteLine(response.Content);
        }
    }
}

当我设置appIdappKey时,我会收到此内容:

{
    "images": [
        {
            "time": 0.19603,
            "status": "Complete",
            "file": "face_55b7d7c4d2008.jpg",
            "width": 1536,
            "height": 2048,
            "faces": [
                {
                    "topLeftX": 300,
                    "topLeftY": 526,
                    "width": 934,
                    "height": 934,
                    "leftEyeCenterX": -1,
                    "leftEyeCenterY": -1,
                    "rightEyeCenterX": -1,
                    "rightEyeCenterY": -1,
                    "noseTipX": -1,
                    "noseTipY": -1,
                    "noseBtwEyesX": -1,
                    "noseBtwEyesY": -1,
                    "chinTipX": -1,
                    "chinTipY": -1,
                    "leftEyeCornerLeftX": -1,
                    "leftEyeCornerLeftY": -1,
                    "leftEyeCornerRightX": -1,
                    "leftEyeCornerRightY": -1,
                    "rightEyeCornerLeftX": -1,
                    "rightEyeCornerLeftY": -1,
                    "rightEyeCornerRightX": -1,
                    "rightEyeCornerRightY": -1,
                    "rightEarTragusX": -1,
                    "rightEarTragusY": -1,
                    "leftEarTragusX": -1,
                    "leftEarTragusY": -1,
                    "leftEyeBrowLeftX": -1,
                    "leftEyeBrowLeftY": -1,
                    "leftEyeBrowMiddleX": -1,
                    "leftEyeBrowMiddleY": -1,
                    "leftEyeBrowRightX": -1,
                    "leftEyeBrowRightY": -1,
                    "rightEyeBrowLeftX": -1,
                    "rightEyeBrowLeftY": -1,
                    "rightEyeBrowMiddleX": -1,
                    "rightEyeBrowMiddleY": -1,
                    "rightEyeBrowRightX": -1,
                    "rightEyeBrowRightY": -1,
                    "nostrilLeftHoleBottomX": -1,
                    "nostrilLeftHoleBottomY": -1,
                    "nostrilRightHoleBottomX": -1,
                    "nostrilRightHoleBottomY": -1,
                    "nostrilLeftSideX": -1,
                    "nostrilLeftSideY": -1,
                    "nostrilRightSideX": -1,
                    "nostrilRightSideY": -1,
                    "lipCornerLeftX": -1,
                    "lipCornerLeftY": -1,
                    "lipLineMiddleX": -1,
                    "lipLineMiddleY": -1,
                    "lipCornerRightX": -1,
                    "lipCornerRightY": -1,
                    "pitch": -1,
                    "yaw": -1,
                    "roll": -1
                }
            ]
        }
    ]
}