title: Forward POST request from asp.net core controller to different URL
body: <p>I wanto to forward an incoming POST request to my asp.net core controller "as is" (including headers, body, from-data) to a different URL without using a middleware.</p>
  
  <p>I found an example for doing that for asp.net: <a href="https://philsversion.com/2012/09/06/creating-a-proxy-with-apicontroller/" rel="nofollow noreferrer">https://philsversion.com/2012/09/06/creating-a-proxy-with-apicontroller/</a></p>
  
  <p>But this does not work for asp.net core, since the call to
      return await http.SendAsync(this.Request);
  in asp.net core accepts an HttpRequestMessage and the Request object is of type HttpRequest.</p>
  
  <p>I also found some code, which creates a HttpRequestMessage from an HttpRequest, see: <a href="https://stackoverflow.com/questions/45759417/convert-microsoft-aspnetcore-http-httprequest-to-httprequestmessage">Convert Microsoft.AspNetCore.Http.HttpRequest to HttpRequestMessage</a></p>
  
  <p>Using this code, the receiving endpoint (to which I forward to) gets the Body, but it does not get Form fields.</p>
  
  <p>Checking the class HttpRequestMessage I saw that it does not contain a property for FormFields.</p>
  
  <pre><code>[Microsoft.AspNetCore.Mvc.HttpPost]
  [NrgsRoute("api/redirect-v1/{key}")]
  public async Task&lt;HttpResponseMessage&gt; Forward(
     [FromUri] string key,
     CancellationToken cancellationToken)
  {
     // the URL was shortened, we need to get the original URL to which we want to forward the POST request
     var url = await _shortenUrlService.GetUrlFromToken(key, cancellationToken).ConfigureAwait(false);
  
       using (var httpClient = new HttpClient())
       {
           var forwardUrl = new Uri(url);
           Request.Path = new PathString(forwardUrl.PathAndQuery);
  
           // see: https://stackoverflow.com/questions/45759417/convert-microsoft-aspnetcore-http-httprequest-to-httprequestmessage
           var requestMessage = Request.ToHttpRequestMessage();
           return await httpClient.SendAsync(requestMessage, cancellationToken);
  
           // Problem: Forwards header and body but NOT form fields
       }
   }
  </code></pre>
  
  <p>Expected result would be that at my receiving endpoint I have the same
  - headers
  - body
  - form fields
  as in the original POST request.</p>