title: How to POST file using WebRequest
body: <p>I want to simulate a transfer file between API server.
  First, I created an API that receive an uploaded a file.
  And successfully upload a file using Postman.
  Here is the Code to receive.</p>
  
  <pre><code>[AllowAnonymous]
  [HttpPost]
  [Route("api/data/UploadFile")]
  public IHttpActionResult UploadFile()
  {
      if (HttpContext.Current.Request.Files.AllKeys.Any())
      {
          var httpPostedFile = HttpContext.Current.Request.Files["UploadedImage"];
  
          if (httpPostedFile != null)
          {
              var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/images"), "Capture1.PNG");
  
              httpPostedFile.SaveAs(fileSavePath);
          }
      }
  
      return Ok("SUCCESS");
  }
  </code></pre>
  
  <p>I tried to upload using postman like so.
  <a href="https://i.stack.imgur.com/YTVxs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YTVxs.png" alt="Postman Upload"></a></p>
  
  <p>Now, I want to upload the file using WebRequest from another API using this code.</p>
  
  <pre><code>[AllowAnonymous]
  [HttpGet]
  [Route("api/data/TestUpload")]
  public IHttpActionResult TestUpload()
  {
      string formdataTemplate = "Content-Disposition: form-data; filename=\"{0}\";\r\nContent-Type: image/png\r\n\r\n";
      string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
      byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
  
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/iisapi/api/data/UploadFile");
      request.ServicePoint.Expect100Continue = false;
      request.Method = "POST";
      request.ContentType = "multipart/form-data; boundary=" + boundary;
  
      string filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/images"), "Capture2.PNG");
  
      using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
      {
          using (Stream requestStream = request.GetRequestStream())
          {
              requestStream.Write(boundarybytes, 0, boundarybytes.Length);
              string formitem = string.Format(formdataTemplate, "UploadedImage");
              byte[] formbytes = Encoding.UTF8.GetBytes(formitem);
              requestStream.Write(formbytes, 0, formbytes.Length);
              byte[] buffer = new byte[fileStream.Length];
              int bytesLeft = 0;
  
              while ((bytesLeft = fileStream.Read(buffer, 0, buffer.Length)) &gt; 0)
              {
                  requestStream.Write(buffer, 0, bytesLeft);
              }
  
          }
      }
  
      try
      {
          using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { }
  
          return Ok("SUCCESS");
      }
      catch (Exception ex)
      {
          throw;
      }
  }
  </code></pre>
  
  <p>If I call the <code>TestUpload</code> API, it successfully enter the <code>UploadFile</code> function. But, when checking if there is files in the request using this code <code>HttpContext.Current.Request.Files.AllKeys.Any()</code>, it return false. There is no File in the request. What am I missing here?</p>
  
  <p>Thank You.</p>