1

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<CASAuthTokenResponse> 
        (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<IActionResult> 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 }}

user3301440
  • 800
  • 6
  • 13
  • 27
  • I see two `HttpClient` objects. Is `client` superfluous to this question? It looks like the meat of the request is done with `client1`. Why is `client` there at all? It's distracting, maybe you should remove it from your question. – Wyck Jun 20 '19 at 13:03
  • 2
    Perhaps the response body will tell you why you're getting a 400 response from the server? – ProgrammingLlama Jun 20 '19 at 13:04
  • i have removed the duplicate code from question – user3301440 Jun 20 '19 at 15:26
  • Any error message from response ? You should check whether JWT token authentication fails or your request is not correct in server side app. – Nan Yu Jun 21 '19 at 07:28

0 Answers0