POST 与 application/octet-stream 通话

POST call with application/octet-stream

您好,我正在尝试触发对以下 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 link.

,此代码工作正常

尝试将请求内容类型设置为"application/octet-stream"。

作为微软的example code given,你能尝试像示例一样设置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);
}