title: The remote server returned an error: (415) Unsupported Media Type
body: <p>I try to upload a text file from <strong>WPF RESTful client to ASP .NET MVC WebAPI 2 website</strong>.</p>
  
  <p><strong>Client code</strong></p>
  
  <pre><code>HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost:22678/api/Account/UploadFile?fileName=test.txt&amp;description=MyDesc1");
  
  request.Method = WebRequestMethods.Http.Post;
  request.Headers.Add("Authorization", "Bearer " + tokenModel.ExternalAccessToken);  
  request.ContentType = "text/plain";
  request.MediaType = "text/plain";
  byte[] fileToSend = File.ReadAllBytes(@"E:\test.txt");  
  request.ContentLength = fileToSend.Length;
  
  using (Stream requestStream = request.GetRequestStream())
  {
        // Send the file as body request. 
        requestStream.Write(fileToSend, 0, fileToSend.Length);
        requestStream.Close();
  }
  
  using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                          Console.WriteLine("HTTP/{0} {1} {2}", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription);
  </code></pre>
  
  <p><strong>WebAPI 2 code</strong></p>
  
  <pre><code>[HttpPost]
  [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
  [Route("UploadFile")]
  public void UploadFile(string fileName, string description, Stream fileContents)
  {
      byte[] buffer = new byte[32768];
      MemoryStream ms = new MemoryStream();
      int bytesRead, totalBytesRead = 0;
      do
      {
          bytesRead = fileContents.Read(buffer, 0, buffer.Length);
          totalBytesRead += bytesRead;
  
          ms.Write(buffer, 0, bytesRead);
      } while (bytesRead &gt; 0);
  
     var data = ms.ToArray() ;
  
      ms.Close();
      Debug.WriteLine("Uploaded file {0} with {1} bytes", fileName, totalBytesRead);
  }
  </code></pre>
  
  <p>So.. Under the client code I am facing that exception</p>
  
  <blockquote>
    <p>The remote server returned an error: (415) Unsupported Media Type.</p>
  </blockquote>
  
  <p>Any clue what do I am missing?</p>