c# - 使用 application/octet-stream 进行 POST 调用

标签 c# rest .net-core azure-cognitive-services

您好,我正在尝试触发对以下 API 的 POST 调用.这是代码

var client = new HttpClient();

client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key);

var queryString = HttpUtility.ParseQueryString(string.Empty);
queryString["returnFaceId"] = "true";
queryString["returnFaceLandmarks"] = "false";
queryString["returnFaceAttributes"] = "age,gender";

var filebytes = File.ReadAllBytes(@"C:\Users\user1\Desktop\IMG.jpg");

var uri = "https://southeastasia.api.cognitive.microsoft.com/face/v1.0/detect?" + queryString;

HttpResponseMessage response;


using (var content = new ByteArrayContent(filebytes))
{
    response = client.PostAsync(uri, content).Result;
    var result = response.Content.ReadAsStringAsync().Result;
}

我在 result 中得到以下错误:

{"error":{"code":"BadArgument","message":"JSON parsing error."}}

如果我使用 application/json 并传递一个 http 链接,这段代码可以正常工作。

最佳答案

作为example code given由 Microsoft 提供,您能否尝试像示例那样设置 ContentType:

using (var content = new ByteArrayContent(byteData))
{
     content.Headers.ContentType = new MediaTypeHeaderValue("< your content type, i.e. application/json >");
     response = await client.PostAsync(uri, content);
}

关于c# - 使用 application/octet-stream 进行 POST 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51838193/

相关文章:

c# - 序列化从 LINQ to SQL 生成的类

java - Swagger 配置 - 访问 swagger.json 时出现 404

docker - 如何在 Ubuntu 14.04 上使用 dotnet CLI 注册新的 NuGet 包源?

c# - aspnet核心无法解析依赖关系: error :'Unable to resolve service for type while attempting to activate'

docker - 来自 Docker 容器的 dotnet restore 失败

c# - C#子字符串引发异常

c# - MSchart 极坐标图自定义标签不起作用

python - 如何根据 Python 中的 OpenAPI3 规范验证 HTTP 请求?

rest - Angular2 REST API

c# - RabbitMQ C# 实例