c# - POST call with application/octet-stream
问题描述
Hi I'm trying to trigger a POST call to the following API. and here's code
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;
}
I get the following error in result
:
{"error":{"code":"BadArgument","message":"JSON parsing error."}}
This code works fine if I use application/json
, and pass a http link.
解决方案
作为微软给出的示例代码,您可以尝试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);
}
推荐阅读
- winforms - 删除图标空间 - 按钮项 DotNetBar
- javascript - Anguarjs:通过异步服务方法更改控制器变量
- tfs - 在 TFS 2010 中创建构建定义时构建过程模板不可见
- python - 迭代不同长度列表并设置默认值的好方法
- discord - 用户离开公会后如何从客户端ID获取用户名
- angular - 子组件销毁通知父组件
- python - 将数据值从 Python 转换为 C
- javascript - 如何使用 promise 调用函数?
- javascript - Selenium + JS:driver.wait(until.elementLocated(By(), 2000)) 有时会卡住
- xpath - XPath 获取除 div 之外的所有内容,包括文本