title: C# POSTing [FromBody] data from one controller to another
body: <p>Is it possible to send <code>[FromBody]</code> POST data to a controller using <code>client.GetAsync()</code> (or PostAsync/SendAsync?</p>
  
  <p>I had to set up a base controller that all api calls will go through.</p>
  
  <p>My ajax calls all go to this <code>SecureApi</code> controller, and they send the original path as a parameter to that they can be re-routed to the correct controller. Something like:</p>
  
  <pre><code>$.ajax({
      url: "./api/SecureApi/?path=/api/OtherApi/SomeRoute",
      data: {
          param1: 1,
          param2: 2
      }
  });
  </code></pre>
  
  <p>So my base controller looks something like:</p>
  
  <pre><code>public class SecurityApiController : ApiController
  {
      [HttpPost]
      public HttpResponseMessage SecureApi([FromBody]object data, string path)
      {
          // do some stuff
  
          // get uri
          var applicationPath = Request.RequestUri.Scheme + "://" + Request.GetRequestContext().VirtualPathRoot.Replace("/", String.Empty);
          Uri routeUri = new Uri(applicationPath + path);
  
          // then redirect to correct controller
          var config = new HttpConfiguration();
          var server = new HttpServer(config);
          var client = new HttpClient(server);
          // how can I send [FromBody]object data here?
          var response = client.GetAsync(routeUri).Result; // or PostAsync/SendAsync?
          return response;
      }
  }
  </code></pre>
  
  <p>The <code>other</code> controller looks like:</p>
  
  <pre><code>public class OtherApiController : ApiController
  {
      [HttpPost]
      public HttpResponseMessage OtherApi([FromBody]OtherData data)
      {
          // do stuff
      }
  }
  </code></pre>
  
  <p>Unfortunately I can't change <code>OtherApi</code>, so I HAVE to send the <code>[FromBody]</code> POST data in the same way (in the POST body).</p>
  
  <p>Is that possible?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Per @Philippe's response below, I'm using PostAsJsonAsync and it seems to want to work, but I'm getting a 401 Unauthorized result. More info:</p>
  
  <p>I went with the correct(?) ASYNC/AWAIT route...</p>
  
  <pre><code>public async Task&lt;HttpResponseMessage&gt; SecureApi([FromBody]Dictionary&lt;string, dynamic&gt; data, string path)
  {
      ...
      var client = new HttpClient();
      var response = await client.PostAsJsonAsync(routePath, data);
      return response;
  }
  </code></pre>
  
  <p>And the <code>Other</code> controller has:</p>
  
  <pre><code>[Authorize(Roles = "Admin")] // I do have the "Admin" role
  [Route("Save")]
  [HttpPost]
  public SaveResultBase Save([FromBody]Dictionary&lt;string, dynamic&gt; data)
  {
      ...
  }
  </code></pre>
  
  <p>But this controller is never hit (no breakpoints are hit there) and it returns a 401 Unauthorized response.</p>
  
  <p>I guess that I have to add my user credentials to the client headers before calling <code>PostAsJsonAsync</code>. Can't find any way to do that though.</p>