title: Xamarin.Android: HttpClient with MultipartFormDataContent returns 400 Bad Request
body: <p>I develop a <strong>Xamarin.Android app</strong> and I need to update an audio recorded file.</p>
  
  <p>For this, my client has provided me an API like this:</p>
  
  <ul>
  <li>URL: /api/recordings/</li>
  <li>Method: POST</li>
  <li>Data format: multipart/form-data</li>
  <li>Fields: some text fields and the audio file to send</li>
  </ul>
  
  <p>When I test it on <strong>Postman</strong>, it <strong>works fine</strong>. The generate code snippets are:</p>
  
  <pre><code>POST /api/recordings/ HTTP/1.1
  Host: X.X.X.X
  Cache-Control: no-cache
  Postman-Token: xxxxx-xxxx-xxxx-xxxx-xxxxx
  Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
  
  ------WebKitFormBoundary7MA4YWxkTrZu0gW
  Content-Disposition: form-data; name="param1" 
  param1 name
  
  ------WebKitFormBoundary7MA4YWxkTrZu0gW
  Content-Disposition: form-data; name="param2"
  param2 name
  
  ------WebKitFormBoundary7MA4YWxkTrZu0gW
  Content-Disposition: form-data; name="audio_file"; filename="xxx.wav"
  Content-Type: audio/wav
  </code></pre>
  
  <p>But when I try <strong>from the app</strong>, I get an <strong>400 - Bad Request error</strong>.</p>
  
  <p><strong>First attempt:</strong></p>
  
  <p>I first tried with the following basic code:</p>
  
  <pre><code>using (HttpClient client = new HttpClient())
  {       
      try
      {
          MultipartFormDataContent formData = new MultipartFormDataContent();
          formData.Add(new StringContent("param1 name"), "param1");
          formData.Add(new StringContent("param2 name"), "param2");
          var response = await client.PostAsync(url, formData);
          Logger.Log("Response : " + responseObj.ToString());
      }
      catch (Exception ex)
      {
          Logger.Log("Exception : " + ex.ToString());
      }
  }
  </code></pre>
  
  <p>But there is a <strong>400 - Bad Request error</strong>:</p>
  
  <pre><code>Response : StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
  {
    Server: nginx/1.15.0
    Date: Mon, 25 Jun 2018 23:25:41 GMT
    Connection: keep-alive
    Vary: Cookie
    X-Frame-Options: SAMEORIGIN
    Content-Type: application/json
    Content-Length: 85
    Allow: POST, OPTIONS
  }
  </code></pre>
  
  <p><strong>Second attempt:</strong></p>
  
  <p>Then I've tried the following snippet that I found <a href="https://stackoverflow.com/questions/21569770/wrong-content-type-header-generated-using-multipartformdatacontent">there</a>:</p>
  
  <pre><code>string servResp = "";
  string boundary = "----CustomBoundary" + DateTime.Now.Ticks.ToString("x");
  using (var content = new MultipartFormDataContent(boundary))
  {
      content.Headers.Remove("Content-Type");
      content.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary);
      content.Add(new StringContent("param1 name"), "param1");
      content.Add(new StringContent("param2 name"), "param2");
  
      HttpClientHandler handler = new HttpClientHandler();
      var cookieContainer = new CookieContainer();
      handler.CookieContainer = cookieContainer;
  
      HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
      request.Headers.ExpectContinue = false;
      request.Content = content;
      try
      {
          var httpClient = new HttpClient(handler);
          HttpResponseMessage response = await httpClient.SendAsync(request);
          response.EnsureSuccessStatusCode();
          servResp = await response.Content.ReadAsStringAsync();
      }
      catch (Exception ex)
      {
          Logger.Log("PostTest() - Exception : " + ex.ToString());
      }
  }
  return servResp;
  </code></pre>
  
  <p>But there is still a <strong>400 - Bad Request error</strong>:</p>
  
  <pre><code>Exception : System.Net.Http.HttpRequestException: 400 (Bad Request)
    at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode () [0x0002a] in &lt;996a681f30a44cd685a4da54e11956e2&gt;:0 
  </code></pre>
  
  <p><strong><em>=> Would you have any explanation? How could I fix this?</em></strong></p>
  
  <p><strong>Edit : the audio file</strong></p>
  
  <p>I haven't specified the code of the audio file, I use a ByteArrayContent in both cases:</p>
  
  <pre><code>byte[] b = System.IO.File.ReadAllBytes(fullFilePath);
  formData.Add(new ByteArrayContent(b, 0, b.Length), "audio_file", "audio.wav");
  </code></pre>
  
  <p><strong>Conclusion:</strong></p>
  
  <p>I was able to resolve my problem by creating a console project that using the same service: this allows me to find and fix my error.</p>
  
  <p>For an other issue, I followed the Fiddler <a href="http://Do%20you%20know%20an%20alternative%20%20to%20Fiddler%20that%20could%20work%20on%20Android%20devices?" rel="nofollow noreferrer">instructions</a> to parameter proxy on the device. However, it seems that the proxy doesn't work for apps but only for websites in the latest versions of Android...</p>
  
  <p><strong><em>Do you know an alternative  to Fiddler that could work on Android devices?</em></strong></p>