C# 为什么我对kairosapi的REST请求说请求缺少必需的参数?
我正在尝试连接到kairos api,以便熟悉它并使用其检测功能 目前还没有用于此api的官方C#库。有一个似乎没有得到积极维护 我正在尝试使用RestSharp直接连接。服务器正在接收我的请求,如果我省略了app_id和app key进行身份验证,它会像我期望的那样响应。因此,该部分(添加标题)似乎起到了作用 添加参数似乎失败了。根据他们的网站:C# 为什么我对kairosapi的REST请求说请求缺少必需的参数?,c#,rest,restsharp,C#,Rest,Restsharp,我正在尝试连接到kairos api,以便熟悉它并使用其检测功能 目前还没有用于此api的官方C#库。有一个似乎没有得到积极维护 我正在尝试使用RestSharp直接连接。服务器正在接收我的请求,如果我省略了app_id和app key进行身份验证,它会像我期望的那样响应。因此,该部分(添加标题)似乎起到了作用 添加参数似乎失败了。根据他们的网站:https://www.kairos.com/docs/face-recognition/唯一需要的参数是带有url或base64编码照片的“图像”
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 + "\"}");
来自我前面提到的同一个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来演示这一点: 文档没有错误,您只需传递
图像
字段,即可完成所有操作
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);
}
}
}
当我在设置了appId
和appKey
的情况下运行此操作时,我会返回以下内容:
{
"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
}
]
}
]
}
谢谢你的回复。我的程序不仅仅是发布的两行lol。我是新来的,说明是只发布绝对必要的内容。我的代码实际上看起来与您的解决方案完全相同,这就是我能够看到响应的方式。回复仍然是一样的,缺少一个必需的参数。@xerotolerant-我更新了我的帖子。您的问题可能是您没有通过网络发送JSON。在我的示例中,您将看到我执行了一个
request.RequestFormat=DataFormat.Json
,然后通过request.AddBody
传递URL。request.RequestFormat
意味着RestSharp将自动将您传递的内容序列化为JSON。谢谢!先生,你真棒!这似乎是两件事的结合,这是我自己无法理解的。我想我的语法对于Json格式是错误的。还有那个AddBody的东西。我真的不知道为什么它是在身体,而不是头球,但你解决了我的问题。谢谢啊,我在请求中有一个逗号,后面只有image
参数,这恰好产生了相同的错误!删除我的答案。。。