title: .NET Core forward a local API form-data post request to remote API
body: <p>I have an AJAX form which post a form-data to a local API url: <code>/api/document</code>. It contains a file and a custom Id. We simply want to take the exact received Request and forward it to a remote API at <code>example.com:8000/document/upload</code>.</p>
  
  <p>Is there a simple way of achieve this "forward" (or proxy?) of the Request to a remote API using Asp.NET Core?</p>
  
  <p>Below we had the idea to simply use Web API Http client to get the request and then resend it (by doing so <strong>we want to be able to for example append a private api key from the backend</strong>), but it seems not to work properly, the <code>PostAsync</code> doesn't accept the <code>Request</code>.</p>
  
  <h2>Raw request sent by Ajax</h2>
  
  <pre><code>POST http://localhost:62640/api/document HTTP/1.1
  Host: localhost:62640
  Connection: keep-alive
  Content-Length: 77424
  Accept: application/json
  Cache-Control: no-cache
  User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
  Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryn1BS5IFplQcUklyt
  Accept-Encoding: gzip, deflate, br
  Accept-Language: en-US,en;q=0.8,fr;q=0.6
  
  ------WebKitFormBoundaryn1BS5IFplQcUklyt
  Content-Disposition: form-data; name="fileToUpload"; filename="test-document.pdf"
  Content-Type: application/pdf
  ...
  ------WebKitFormBoundaryn1BS5IFplQcUklyt
  Content-Disposition: form-data; name="id"
  
  someid
  ------WebKitFormBoundaryn1BS5IFplQcUklyt--
  </code></pre>
  
  <h2>Backend Code</h2>
  
  <p>Our .NET Core backend has a simple "forward to another API" purpose.</p>
  
  <pre><code>public class DocumentUploadResult
  {
       public int errorCode;
       public string docId;
  }
  
  [Route("api/[controller]")]
  public class DocumentController : Controller
  {
      // POST api/document
      [HttpPost]
      public async Task&lt;DocumentUploadResult&gt; Post()
      {
          client.BaseAddress = new Uri("http://example.com:8000");
  
          client.DefaultRequestHeaders.Accept.Clear();
          HttpResponseMessage response = await client.PostAsync("/document/upload", Request.Form);
          if (response.IsSuccessStatusCode)
          {
              retValue = await response.Content.ReadAsAsync&lt;DocumentUploadResult&gt;();
          }
          return retValue;
      }
  }
  </code></pre>
  
  <p>We have a GET request (not reproduced here) which works just fine. As it doesn't have to fetch data from locally POSTed data.</p>
  
  <h2>My question</h2>
  
  <p><strong>How to simply pass the incoming local HttpPost request and forwarding it to the remote API?</strong></p>
  
  <p>I searched A LOT on stackoverflow or on the web but all are old resources talking about forwarding <code>Request.Content</code> to the remote. </p>
  
  <p>But on Asp.NET Core 1.0, we don't have access to <code>Content</code>. We only are able to retrieve <code>Request.Form</code> (nor <code>Request.Body</code>) which is then not accepted as an argument of <code>PostAsync</code> method: </p>
  
  <blockquote>
    <p>Cannot convert from Microsoft.AspNetCore.Http.IformCollection to
    System.Net.Http.HttpContent</p>
  </blockquote>
  
  <p>I had the idea to directly pass the request to the postAsync:</p>
  
  <blockquote>
    <p>Cannot convert from Microsoft.AspNetCore.Http.HttpRequest to
    System.Net.Http.HttpContent</p>
  </blockquote>
  
  <p><strong>I don't know how to rebuild expected <code>HttpContent</code> from the local request I receive.</strong></p>
  
  <h2>Expected response</h2>
  
  <p>For information, When we post a valid form-data with the custom <code>Id</code> and the uploaded file, the remote (example.com) API response is:</p>
  
  <pre><code>{
    "errorCode": 0
    "docId": "585846a1afe8ad12e46a4e60"
  }
  </code></pre>