title: How to construct HttpClient POST Request with form-data in C#?
body: <p>I am having issues constructing the POST request with form-data in C# using HTTPClient. i tried multiple approaches but nothing seems to work. Can you please help what I am missing?
  <p>Please be informed, the SWAGGER POST is working fine and based on that I am trying to construct the request.</p>
  
  <p>The API parameters</p>
  
  <pre><code>rPath* string
  (formData)
  nName* string
  (formData)
  nFile string
  (formData)
  type* string
  (formData)  
  zFile file
  file
  </code></pre>
  
  <p>The working swagger POST</p>
  
  <pre><code>curl -X POST "http://localhost:8888/server/api/v1.0/createfolder" -H "accept: text/plain; charset=UTF-8" -H "authorization: Basic XXXXXXX" -H "Content-Type: multipart/form-data" -F "rPath=/RootFolder/" -F "nName=testing" -F "type=Folder"
  </code></pre>
  
  <p>The Fiddler request header of the SWAGGER POST (This is working).</p>
  
  <p><a href="https://i.stack.imgur.com/jloU8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jloU8.png" alt="Fiddler Request details"></a></p>
  
  <pre><code>    static void Main(string[] args)
      {
          var formData = new MultipartFormDataContent();
          HttpContent content = new FormUrlEncodedContent (new[]
          {
              new KeyValuePair&lt;string, string&gt;("rPath",  "/RootFolder/"),
              new KeyValuePair&lt;string, string&gt;("nName", "TestFolder"),
              new KeyValuePair&lt;string, string&gt;("type", "Folder")
          });           
          content.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");
          content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
           formData.Add(content);
          var url = "http://localhost:8888/server/api/v1.0/createfolder";
          HttpResponseMessage response = PostRoot(formData, url);
      }
      static HttpResponseMessage PostRoot(HttpContent content, string webMethod)
      {
          HttpResponseMessage response = new HttpResponseMessage();
          using (var client = new HttpClient() {  })
          {
              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
              client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "xxxxxxx=");
              response = client.PostAsync(webMethod, content).Result;
          }
  
          return response;
      }
  </code></pre>