RAGHUNATH

RAGHUNATH

  • 1.7k
  • 63
  • 49.9k

HttpClient POST to Web API returns 400 Bad Request

Jun 21 2019 12:46 AM
I am trying to post captured image from WPF to WebApi method using HttpClient but i am getting 400 BAD REQUEST error.
I have tried in google but unable to resolve the issue. does anyone help me.
 
Below is the code in WPF
 
private async void btnLogin_Click(object sender, RoutedEventArgs e)
{
string FileName =
System.IO.Path.GetFullPath("../../captured_images") +
"//captured_image" + DateTime.Now.Day.ToString() +
DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() +
DateTime.Now.Second.ToString() + ".jpg";
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)image.Source));
using (FileStream stream = new FileStream(FileName,
FileMode.Create))
encoder.Save(stream);
string CASAAuthResponse = await
CASSecurity.GetAuthenticationToken();
CASAuthTokenResponse techSeeTokenResponse =
JsonConvert.DeserializeObject
(CASAAuthResponse);
HttpContent fileStreamContent = new StreamContent(File.OpenRead(FileName));
using (var client1 = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
client1.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
formData.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
formData.Add(fileStreamContent, "face",
Path.GetFileName(FileName));
var response = await
client1.PostAsync(CASIdentifyFaceUrl, formData);
if (!response.IsSuccessStatusCode)
{
return null;
}
}
}
 
Server Web api:
 
[HttpPost]
[Route("identify")]
public async Task Identify(IFormFile face)
{
Guid temporaryUsername = Guid.Empty;
using (var faceStream = face.OpenReadStream())
{
temporaryUsername = await verifyBusiness.IdentifyUser(faceStream,
new Guid(Requester.ClientId));
}
return Ok(temporaryUsername);
}
 
> And i am getting error as descibed below:
 
{StatusCode: 400,
> ReasonPhrase: 'Bad Request', Version: 1.1, Content:
> System.Net.Http.StreamContent, Headers: { Transfer-Encoding: chunked
> Strict-Transport-Security: max-age=2592000 Date: Thu, 20 Jun 2019
> 11:13:28 GMT Set-Cookie:
> ARRAffinity=4cbc3e777eee0146fcbb9f695794b29417cc953731f6f8f581457a1d7cd7aa14;Path=/;HttpOnly;Domain=cas-qa.tempdata.net
> Server: Kestrel X-Powered-By: ASP.NET Content-Type:
> application/json; charset=utf-8 }}

Answers (1)