人脸API Base64图片post returns badrequest
Face API Base64 image post returns badrequest
Face API 适用于图像的 URL 但是当我尝试将图像发送为
Base64 编码 returns 错误。
在此示例代码中,我使用 Restsharp 发送数据
我的代码如下。
public string GenerateRest(String Base64Image)
{
var serviceURL = Constants.BaseServiceURI;
var client = new RestClient(serviceURL);
var request = new RestRequest(Constants.BaseResoruce, Method.POST);
request.AddHeader("Ocp-Apim-Subscription-Key", Constants.MS_API_KEY);
request.AddHeader("Content-Type", "application/octet-stream");
request.RequestFormat = DataFormat.Json;
var baseString = Base64Image.Replace("data:image/jpeg;base64,", String.Empty);
byte[] newBytes = Convert.FromBase64String(baseString);
request.AddBody(newBytes);
// execute the request
IRestResponse response = client.Execute(request);
var content = response.Content; // raw content as string
return content;
}
根据答案代码修复 Restsharp 请求后就像
public FaceAPIOutput GenerateRest(String Base64Image)
{
var serviceURL = Constants.BaseServiceURI;
var client = new RestClient(serviceURL);
var baseString = Base64Image.Replace("data:image/jpeg;base64,", String.Empty);
byte[] newBytes = Convert.FromBase64String(baseString);
var request = new RestRequest(Constants.BaseResoruce, Method.POST);
request.AddHeader("Ocp-Apim-Subscription-Key", Constants.MS_API_KEY);
request.AddParameter("application/octet-stream", newBytes, ParameterType.RequestBody);
request.RequestFormat = DataFormat.Json;
IRestResponse response = client.Execute(request);
var content = response.Content; // raw content as string
return ConvertToFaceAPIOutObject(content);
}
Restsharp 在采用二进制有效负载时看起来有点特殊。您需要执行以下操作,而不是添加多部分表单部分的 request.AddBody
:
request.AddParameter("application/octet-stream", newBytes, ParameterType.RequestBody);
c.f。 Can RestSharp send binary data without using a multipart content type?
Face API 适用于图像的 URL 但是当我尝试将图像发送为 Base64 编码 returns 错误。 在此示例代码中,我使用 Restsharp 发送数据
我的代码如下。
public string GenerateRest(String Base64Image)
{
var serviceURL = Constants.BaseServiceURI;
var client = new RestClient(serviceURL);
var request = new RestRequest(Constants.BaseResoruce, Method.POST);
request.AddHeader("Ocp-Apim-Subscription-Key", Constants.MS_API_KEY);
request.AddHeader("Content-Type", "application/octet-stream");
request.RequestFormat = DataFormat.Json;
var baseString = Base64Image.Replace("data:image/jpeg;base64,", String.Empty);
byte[] newBytes = Convert.FromBase64String(baseString);
request.AddBody(newBytes);
// execute the request
IRestResponse response = client.Execute(request);
var content = response.Content; // raw content as string
return content;
}
根据答案代码修复 Restsharp 请求后就像
public FaceAPIOutput GenerateRest(String Base64Image)
{
var serviceURL = Constants.BaseServiceURI;
var client = new RestClient(serviceURL);
var baseString = Base64Image.Replace("data:image/jpeg;base64,", String.Empty);
byte[] newBytes = Convert.FromBase64String(baseString);
var request = new RestRequest(Constants.BaseResoruce, Method.POST);
request.AddHeader("Ocp-Apim-Subscription-Key", Constants.MS_API_KEY);
request.AddParameter("application/octet-stream", newBytes, ParameterType.RequestBody);
request.RequestFormat = DataFormat.Json;
IRestResponse response = client.Execute(request);
var content = response.Content; // raw content as string
return ConvertToFaceAPIOutObject(content);
}
Restsharp 在采用二进制有效负载时看起来有点特殊。您需要执行以下操作,而不是添加多部分表单部分的 request.AddBody
:
request.AddParameter("application/octet-stream", newBytes, ParameterType.RequestBody);
c.f。 Can RestSharp send binary data without using a multipart content type?