【发布时间】:2017-08-04 10:34:37
【问题描述】:
我正在使用 Microsoft Azure 认知服务开展人脸识别项目。不太清楚为什么我不能更正我自己的 JSON 格式错误的语法,我认为我在 6 个月前就已经确定了这一点。我想创建一个组名,所以我调用“Person Group API”,每次我按照 MS 示例进行操作时,我的代码都会出错,但是在 API 测试控制台中没有问题,这是我从 MS 站点借用的代码示例:
{ "error": { "code": "ResourceNotFound", "message": "The requested resource was not found." } }
以及在控制台模式下运行的代码:
static async void CreateGroup()
{
string key1 = "YourKey";
// azure the one should work
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
// Request headers
client.DefaultRequestHeaders.Add
("Ocp-Apim-Subscription-Key", key1);
var uri = "https://westus.api.cognitive.microsoft.com/face/v1.0/
persongroups/{personGroupId}?" + queryString;
HttpResponseMessage response;
// Request body
string groupname = "myfriends";
string body = "{\"name\":\"" + groupname + ","+ "\"}";
// Request body
using (var content = new StringContent
(body, Encoding.UTF8, "application/json"))
{
await client.PostAsync(uri, content)
.ContinueWith(async responseTask =>
{
var responseBody = await responseTask.Result
.Content.ReadAsStringAsync();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Response: {0}", responseBody);
Console.WriteLine("");
Console.WriteLine("Group Created.... ");
Console.WriteLine("Hit ENTER to exit...");
Console.ReadKey();
});
response = await client.PutAsync(uri, content);
Console.WriteLine("what is this {0}", response.ToString());
Console.ReadKey();
}// end of using statement
}// end of CreateGroup
#endregion
我猜在这里,但我认为我的 JSON 格式再次错误,我只是不知道这次我又做错了什么。根据该站点,我需要发送给 ms 的字段名称是 'name' : 'userData' 是可选的。
【问题讨论】: