title: How to upload an XML file using multipart/form-data with Restsharp?
body: <p><strong>Question:</strong> How to upload an XML file using multipart/form-data with Restsharp?</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>I'm using Peppol for sending invoices using the Codabox API.<br>
  I want to upload an xml to the rest service. <br>
  The rest service itself is under control by the provider Codabox.<br>
  I have 2 methods provided who I expect to do the same.</p>
  
  <p>First of all with Postman and httpclient, all the things works fine.
  I want to get the same from the httpclient method working using the restsharp way.</p>
  
  <p>RestSharp version: 106.2.1</p>
  
  <blockquote>
    <p><strong>Error message with Restsharp</strong></p>
    
    <p>response = "StatusCode: BadRequest, Content-Type: application/json,
    Content-Length: -1)" Content = "{\"file\":[\"No file was
    submitted.\"]}"</p>
  </blockquote>
  
  <p>For realizing this I have an X-Software-Company key in the header, providing a valid xml file that I send using form-data (multipart/form-data) and my authentication credentials.</p>
  
  <p><strong>Expected solution:</strong> </p>
  
  <p>I want to get the Restsharp method working and why it now doesn't work.
  So the Restsharp method I provided need to do the same as the httpclient method I provided.</p>
  
  <p><strong>What I have tried:</strong></p>
  
  <p><strong>Restsharp method:</strong> ==> here is the problem </p>
  
  <pre><code>  public void TestUpload()
      {
          byte[] fileBytes = File.ReadAllBytes(@"C:\temp\test.xml");
  
          var client = new RestClient("url for the rest call");
  
          var request = new RestRequest(Method.POST);
          request.AlwaysMultipartFormData = true;
  
          request.Credentials = new NetworkCredential("username", "password");
  
          request.AddHeader("X-Software-Company", "software key");
          request.AddHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
          request.AddFile("file", @"C:\temp\test.xml");
          //request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
          //request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\temp\\test.xml\"\r\nContent-Type: false\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
          IRestResponse response = client.Execute(request);
      }
  </code></pre>
  
  <p><strong>HttpClient method:</strong> ==> it works fine</p>
  
  <pre><code>public void TestUploadHttpClient()
      {
          byte[] fileBytes = File.ReadAllBytes(@"C:\temp\test.xml");
  
          using (HttpClient httpClient = new HttpClient())
          {
              httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "credentials");
              httpClient.DefaultRequestHeaders.Add("X-Software-Company", "software key");
              using (var content = new MultipartFormDataContent("boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"))
              {
                  content.Add(new StreamContent(new MemoryStream(fileBytes)), "file", "test.xml");
  
                  using (var message = httpClient.PostAsync("url for the rest call", content).Result)
                  {
                      var input = message.Content.ReadAsStringAsync().Result;
                  }
              }
          }
      }
  </code></pre>
  
  <p><strong>Postman generated code:</strong></p>
  
  <p>If I do the request by Postman there is no problem, if I check the Restsharp code generated by postman it gives me: </p>
  
  <pre><code>var client = new RestClient("url for the rest call");
  var request = new RestRequest(Method.POST);
  request.AddHeader("Authorization", "Basic credentials");
  request.AddHeader("Content-Type", "multipart/form-data");
  request.AddHeader("X-Software-Company", "software key");
  request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
  request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\temp\\test.xml\"\r\nContent-Type: false\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
  IRestResponse response = client.Execute(request);
  </code></pre>
  
  <p>I have exactly the code tested generated from postman but it doesn't work.</p>
  
  <p><strong>EDIT 2018-03-19:</strong> <br><br>
  Possible issue in RestSharp: <a href="https://github.com/restsharp/RestSharp/issues/1079" rel="nofollow noreferrer">Added files not being recieved #1079</a></p>
  
  <p><strong>Temporary solution:</strong> </p>
  
  <p>I'm using RestSharp version v105.2.3 then it works like a charm.</p>
  
  <p>Have anyone an idea why the restsharp method does not work and how to solve that?</p>