title: Trouble including a file in multipart/form-data POST using RestSharp
body: <p>I'm relatively new to web interactions with C# and I'm having some trouble making a POST request to upload a file using an API. The API only accepts the files as part of a multipart/form-data section in the body. At other's suggestions I've been trying to use RestSharp to do this, but I can't seem to get the file itself into the POST. Chunks of code derived from Postman suggested code - where the POST works.</p>
  
  <p>I've tried a few things. This chunk resulted in a POST going through with the correct parameter in the body, but no file was included. </p>
  
  <pre><code>var client = new RestClient(postURL);
  var request = new RestRequest(Method.POST);
  request.AddHeader("cache-control", "no-cache");
  request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
  request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
  request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", string.Format("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"upfile\"; filename=\"{0}\"\r\nContent-Type: application/xml\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"overwrite\"\r\n\r\ntrue\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", xmlPath), ParameterType.RequestBody);
  IRestResponse response = client.Execute(request);
  string test = response.Content.ToString();
  </code></pre>
  
  <p>I also tried some variations of AddFile - physical file path, byte array, and byte array with content type = application/xml. With these iterations, I was able to get a file to post, but the overwrite parameter wasn't coming through correctly to force a file overwrite.</p>
  
  <pre><code>var client = new RestClient(postURL);
  var request = new RestRequest(Method.POST);
  request.AddHeader("cache-control", "no-cache");
  request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
  request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
  // The different part below
  request.AddFile("upfile", @xmlPath);
  request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"overwrite\"\r\n\r\ntrue\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
  IRestResponse response = client.Execute(request);
  string test = response.Content.ToString();
  </code></pre>
  
  <p>*note: I'm using an older version of RestSharp (105.2.3) to be compatible with .Net 4.0 (stuck with it in this case).</p>
  
  <p>Any ideas as to what I'm doing wrong here?</p>