title: Unable to upload the image with MultipartFormDataContent C#
body: <p>I have the requirement to upload a certain image to the server with help of API. But, it always failed with a bad request/not acceptable error message. But, I can able to upload the same image with the postman. Here, I will attach both the result.</p>
  <p>Postman Auto generated code :</p>
  <pre><code>var client = new RestClient(&quot;http://localhost:3000/api/calendar/thumb/photo&quot;);
  client.Timeout = -1;
  var request = new RestRequest(Method.POST);
  request.AddHeader(&quot;Content-Type&quot;, &quot;application/json&quot;);
  request.AddHeader(&quot;Authorization&quot;, &quot;Bearer xjmwn4WL5H_3VsfhdsghdsKHGFQ-L-gjAmoN&quot;);
  request.AddHeader(&quot;Cookie&quot;, &quot;__profilin=p%3Dt&quot;);
  request.AddFile(&quot;image[file]&quot;, &quot;/C:/Users/antony/Desktop/Avatar.jpg&quot;);
  request.AddParameter(&quot;Content-Type&quot;, &quot;multipart/form-data&quot;);
  IRestResponse response = client.Execute(request);
  Console.WriteLine(response.Content);
  </code></pre>
  <p>The content received on the server-side while posting through the postman,</p>
  <pre><code>&lt;ActionController::Parameters {&quot;image&quot;=&gt;&lt;ActionController::Parameters {&quot;file&quot;=&gt;#&lt;ActionDispatch::Http::UploadedFile:0x00007fffd55f0af0 @tempfile=#&lt;Tempfile:/tmp/RackMultipart20210401-10712-x81lh3.jpg&gt;, @original_filename=&quot;Avatar.jpg&quot;, @content_type=&quot;image/jpeg&quot;, @headers=&quot;Content-Disposition: form-data; name=\&quot;image[file]\&quot;; filename=\&quot;Avatar.jpg\&quot;\r\nContent-Type: image/jpeg\r\n&quot;&gt;} permitted: false&gt;, &quot;Content-Type&quot;=&gt;&quot;multipart/form-data&quot;, &quot;controller&quot;=&gt;&quot;calendar&quot;, &quot;action&quot;=&gt;&quot;upload_photo_api&quot;} permitted: false&gt;
  </code></pre>
  <p>The above one was working perfectly.</p>
  <p>And, I have written a similar kind of code in my c# application. I have used the below function to upload the photo.</p>
  <pre><code>private async Task&lt;bool&gt; uploadPhoto(string email, string data, string hash) {
  
      byte[] photo = Encoding.ASCII.GetBytes(data);
      HttpContent content = new ByteArrayContent(photo, 0, photo.Count());
      content.Headers.ContentType = MediaTypeHeaderValue.Parse(&quot;image/jpeg&quot;);
      content.Headers.ContentDisposition = new ContentDispositionHeaderValue(&quot;form-data&quot;) {
          FileName = string.Concat(email, &quot;_&quot;, hash, &quot;.jpg&quot;),
          Name = &quot;image[file]&quot;,
      };
  
      MultipartFormDataContent postContent = new MultipartFormDataContent(DateTime.Now.ToString());
      postContent.Add(content);
      Httpwrapper service = cService.ConfigureServices().GetRequiredService&lt;Httpwrapper&gt;();
  
      string apiRes = await service.SendAsync(&quot;api/calendar/thumb/photo&quot;, postContent, HttpMethod.Post);
      return apiRes != string.Empty;
  }
  </code></pre>
  <p>And, my httpClient as like below</p>
  <pre><code>HttpClient client;
  client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(&quot;application/json&quot;));
  client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(&quot;Bearer&quot;, &quot;xjmwn4WL5H_3VsfhdsghdsKHGFQ-L-gjAmoN&quot;);
  </code></pre>
  <p>This code sends the data to the server as like below,</p>
  <pre><code>&lt;ActionController::Parameters {&quot;image&quot;=&gt;&lt;ActionController::Parameters {&quot;file&quot;=&gt;#&lt;ActionDispatch::Http::UploadedFile:0x00007f0e04ee4530 @tempfile=#&lt;Tempfile:/tmp/RackMultipart20210402-10712-tuf0rm.jpg&gt;, @original_filename=&quot;anton@testdomain.com_58-A0-7F-41-58-09-13-06-8D-3E-48-77-73-39-08-AD.jpg&quot;, @content_type=&quot;image/jpeg&quot;, @headers=&quot;Content-Type: image/jpeg\r\nContent-Disposition: form-data; filename=\&quot;anton@testdomain.com_58-A0-7F-41-58-09-13-06-8D-3E-48-77-73-39-08-AD.jpg\&quot;; name=\&quot;image[file]\&quot;\r\n&quot;&gt;} permitted: false&gt;, &quot;controller&quot;=&gt;&quot;calendar&quot;, &quot;action&quot;=&gt;&quot;upload_photo_api&quot;} permitted: false&gt;
  </code></pre>
  <p>But, This does not work and it's through the error message bad_request or not_acceptable.</p>
  <p>I see the <strong>&quot;Content-Type&quot;=&gt;&quot;multipart/form-data&quot;</strong> was the only difference between both the request. The request send through the postman has the <strong>&quot;Content-Type&quot;=&gt;&quot;multipart/form-data&quot;</strong> and the request send through the code does not have that <strong>&quot;Content-Type&quot;=&gt;&quot;multipart/form-data&quot;</strong>. But, I'm not sure how to include that.</p>
  <p>What am I missing?</p>