title: HttpClient uploading MultipartFormData to play 2 framework
body: <p>I have the following code in a Windows Phone 8 project that uses RestSharp client:</p>
  
  <pre><code>public async Task&lt;string&gt; DoMultiPartPostRequest(String ext, JSonWriter jsonObject, ObservableCollection&lt;Attachment&gt; attachments)
      {
          var client = new RestClient(DefaultUri);
          // client.Authenticator = new HttpBasicAuthenticator(username, password);
  
          var request = new RestRequest(ext, Method.POST);
  
          request.RequestFormat = DataFormat.Json;
          request.AddParameter("json", jsonObject.ToString(), ParameterType.GetOrPost);
  
          // add files to upload
          foreach (var a in attachments)
              request.AddFile("attachment", a.FileBody, "attachment.file", a.ContType);
  
          var content = await client.GetResponseAsync(request);
  
          if (content.StatusCode != HttpStatusCode.OK)
              return "error";
  
          return content.Content;
      }
  </code></pre>
  
  <p>Fiddler shows the generated header:</p>
  
  <pre><code>POST http://192.168.1.101:9000/rayz/create HTTP/1.1
  Content-Type: multipart/form-data; boundary=-----------------------------28947758029299
  Content-Length: 71643
  Accept-Encoding: identity
  Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
  User-Agent: RestSharp 104.1.0.0
  Host: 192.168.1.101:9000
  Connection: Keep-Alive
  Pragma: no-cache
  
  -------------------------------28947758029299
  Content-Disposition: form-data; name="json"
  
  {
      "userId": "2D73B43390041E868694A85A65E47A09D50F019C180E93BAACC454488F67A411",
      "latitude": "35.09",
      "longitude": "33.30",
      "accuracy": "99",
      "maxDistance": "dist",
      "Message": "mooohv"
  }
  -------------------------------28947758029299
  Content-Disposition: form-data; name="attachment"; filename="attachment.file"
  Content-Type: image/jpeg
  
  ?????JFIF??`?`?????C?  $" &amp;0P40,,0bFJ:Ptfzxrfpn????????np????????|????????????C"$$0*0^44^?p??????????????????????????????????????????????????????`?"??????????????
  -------------------------------28947758029299
  </code></pre>
  
  <p>The code above works fine on the Play2 API. However since the RestSharp does not seem to be stable I have decided to use the native HttpClient provided by Microsoft.</p>
  
  <p>Hence I wrote another function that uses HttpClient to do the same job:</p>
  
  <pre><code>public async Task&lt;string&gt; DoMultiPartPostRequest2(String ext, JSonWriter jsonObject,
                                                                   ObservableCollection&lt;Attachment&gt; attachments)
      {
          var client = new HttpClient();
  
          var content = new MultipartFormDataContent();
  
          var json = new StringContent(jsonObject.ToString());
          content.Add(json, "json");
  
          foreach (var a in attachments)
          {
              var fileContent = new StreamContent(new MemoryStream(a.FileBody));
              fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
              {
                  Name = "attachment",
                  FileName = "attachment.file"
              };
              fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(a.ContType);
              content.Add(fileContent);
          }
  
          var resp = await client.PostAsync(DefaultUri + ext, content);
  
          if (resp.StatusCode != HttpStatusCode.OK)
              return "error";
  
          var reponse = await resp.Content.ReadAsStringAsync();
  
          return reponse;
      }
  </code></pre>
  
  <p>The header that is generated from that code is the following:</p>
  
  <pre><code>POST http://192.168.1.101:9000/rayz/create HTTP/1.1
  Accept: */*
  Content-Length: 6633
  Accept-Encoding: identity
  Content-Type: multipart/form-data; boundary="e01b2196-d24a-47a2-a99b-e82cc4a2f92e"
  User-Agent: NativeHost
  Host: 192.168.1.101:9000
  Connection: Keep-Alive
  Pragma: no-cache
  
  --e01b2196-d24a-47a2-a99b-e82cc4a2f92e
  Content-Type: text/plain; charset=utf-8
  Content-Disposition: form-data; name=json
  
  {
      "userId": "2D73B43390041E868694A85A65E47A09D50F019C180E93BAACC454488F67A411",
      "latitude": "35.09",
      "longitude": "33.30",
      "accuracy": "99",
      "maxDistance": "dist",
      "Message": "test"
  }
  --e01b2196-d24a-47a2-a99b-e82cc4a2f92e
  Content-Disposition: form-data; name=attachment; filename=attachment.file
  Content-Type: image/jpeg
  
  ?????JFIF??`?`?????C?  $" &amp;0P40,,0bFJ:Ptfzxrfpn????????np????????|????????????C"$$0*0^44^?p????????????????????????????????????????????????????????"??????????????
  --e01b2196-d24a-47a2-a99b-e82cc4a2f92e--
  </code></pre>
  
  <p>So far so good. From my point of view the two headers seem to be identical.</p>
  
  <p>However when I debug the Play 2 API after executing <code>Http.MultipartFormData body = request().body().asMultipartFormData();</code> I noticed that the multipart data are not being parsed correctly.</p>
  
  <p>More specifically the multipart filed in the body variable is as follows:</p>
  
  <pre><code>MultipartFormData(Map(),List(),List(BadPart(Map(ntent-type -&gt; text/plain; charset=utf-8, content-disposition -&gt; form-data; name=json)), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map())),List())
  </code></pre>
  
  <p>As you can notice it has several (actually 5 in this example) BadParts.
  Example: <code>BadPart(Map(ntent-type -&gt; text/plain; charset=utf-8, content-disposition -&gt; form-data; name=json))</code></p>
  
  <p>Can anyone see what is going wrong here? Is the header generated by HttpClient wrong?</p>