title: Uploading multipart form files with HttpClient
body: <p>I am trying to create an attachment using the Support Bee API as documented here:
  <a href="https://supportbee.com/api#create_attachment" rel="nofollow noreferrer">https://supportbee.com/api#create_attachment</a></p>
  
  <p>I have written a service that uses an <code>HttpClient</code> to create and send the request using a filename.</p>
  
  <p>If I test in in Postman, it succeeds. I am using <code>form-data</code> for the body and just selecting the file to upload from the UI:</p>
  
  <p><a href="https://i.stack.imgur.com/tNVQ7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tNVQ7.png" alt="enter image description here"></a></p>
  
  <p>It doesn't work when I try to upload it via my <code>HttpClient</code> Service:</p>
  
  <pre><code>public async Task&lt;string&gt; CreateAttachmentAsync(string fileName)
  {
      // "client" is HttpClient provided via D.I.
  
      MultipartFormDataContent content = new MultipartFormDataContent();
      content.Add(new StreamContent(new FileStream(fileName, FileMode.Open)), "files[]");
  
      using (HttpResponseMessage response = await client.PostAsync(
          "https://xxx.supportbee.com/attachments?auth_token=xxx",
          content))
      {
          string responseString = await response.Content.ReadAsStringAsync();
  
          return responseString;
      }
  }
  </code></pre>
  
  <p>This results in a 500 Internal Server Error. Inspecting the <code>MultipartFormDataContent</code> object I can see that it's header values are automatically being set:</p>
  
  <blockquote>
    <p>{
        Content-Type: multipart/form-data; boundary="c9be3778-4de5-4460-9929-adcaa6bdda79"
        Content-Length: 164
    }</p>
  </blockquote>
  
  <p>I have also tried reading the file to a byte array first and using <code>ByteArrayContent</code> instead of <code>StreamContent</code> to no avail. The response doesn't provide anything helpful, but since the request works in Postman I must have something wrong with my code, but I don't know what else to try.</p>
  
  <p>Edit: I tested with Fiddler to compare the successful Postman request to my code. Here is the request with Postman:</p>
  
  <blockquote>
    <p>POST
    <a href="https://xxx.supportbee.com/attachments?auth_token=xxx" rel="nofollow noreferrer">https://xxx.supportbee.com/attachments?auth_token=xxx</a>
    HTTP/1.1 User-Agent: PostmanRuntime/7.22.0 Accept: <em>/</em> Cache-Control:
    no-cache Postman-Token: f84d22fa-b4b1-4bf5-b183-916a786c6385 Host:
    xx.supportbee.com Content-Type: multipart/form-data;
    boundary=--------------------------714700821471353664787346
    Accept-Encoding: gzip, deflate, br Content-Length: 241 Connection:
    close</p>
    
    <p>----------------------------714700821471353664787346 Content-Disposition: form-data; name="files[]"; filename="sample.txt"
    Content-Type: text/plain</p>
    
    <p>This contains example text.
    ----------------------------714700821471353664787346--</p>
  </blockquote>
  
  <p>And the failing request from my code:</p>
  
  <blockquote>
    <p>POST
    <a href="https://xxx.supportbee.com/attachments?auth_token=xxx" rel="nofollow noreferrer">https://xxx.supportbee.com/attachments?auth_token=xxx</a>
    HTTP/1.1 Host: xxx.supportbee.com Accept: <em>/</em> Accept-Encoding:
    gzip, deflate, br Connection: close Content-Type: multipart/form-data;
    boundary="ea97cbc1-70ea-4cc4-9801-09f5feffc763" Content-Length: 206</p>
    
    <p>--ea97cbc1-70ea-4cc4-9801-09f5feffc763 Content-Disposition: form-data; name="files[]"; filename=sample; filename*=utf-8''sample</p>
    
    <p>This contains example text.
    --ea97cbc1-70ea-4cc4-9801-09f5feffc763--</p>
  </blockquote>
  
  <p>The difference I can see is that the individual part in Postman has its own <code>Content-Type: text/plain</code> header for the file, and mine doesn't. I'm unable to add this because if I try <code>content.Headers.Add("Content-Type", "text/plain");</code> It fails with 'Cannot add value because header 'Content-Type' does not support multiple values.'</p>