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

标签 c# rest restsharp

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

此 api 尚无官方 C# 库。有一个似乎没有得到积极维护。

我正在尝试使用 RestSharp 直接连接。服务器正在接收我的请求,如果我遗漏了用于身份验证的 app_id 和应用程序 key ,它会像我期望的那样响应。所以那部分(添加标题)似乎有效。

添加参数似乎失败了。根据他们的网站:https://www.kairos.com/docs/face-recognition/唯一需要的参数是带有 url 或 base64 编码照片的“图像”。

我添加了参数:

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

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

但响应仍然是:“错误代码:1002”, “消息”:缺少一个或多个必需参数。

我怀疑 uri 的构造方式存在问题,但我真的无法确定。我复制了语法

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

来 self 之前提到的同一个 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);


    }

最佳答案

在发送之前,您必须将参数序列化为 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
                }
            ]
        }
    ]
}

关于c# - 为什么我对 Kairos api 的 REST 请求说请求缺少必需的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31683819/

相关文章:

c# - 如何获取Revit中的所有参数选择?

java - 如何在 Eclipse 外部的远程 tomcat 服务器上部署 Web 项目,其中包含 .jar 文件

.net-4.0 - 如何使用 RESTSharp 和 .net 4.0 将文件上传到机架空间?

REST 和 URI 缓存

.net - 如何在 RestSharp 中向请求正文添加文本

c# - 打包 URL 和单元测试。我的环境有问题?

C# - 如何将 Lazy<List<T>> 转换为 Lazy<List<U>>?

c# - 按(可能不唯一的)值对哈希表进行排序

Java休息服务器: make a unit test