title: Receiving 500 error posting valid JSON to .NET Core 2 / .NET MVC Api. (post request is null and stating "System.Net.Http.NoWriteNoSeekStreamContent")
body: <p>So quick background on what is currently going on in my solution. I'm writing pretty basic API web tests in c# using the XUnit framework and some json validation helpers I created to validate response messages and payloads received back from the API. I've been banging my head against a wall trying to figure out why my PostAsync() call is passing null once it reaches the API since I can see the request body is a valid json request when debugging my ApiTests solution. Below are some snippets of the test I'm currently debugging.</p>
  
  <p><strong>OrderControllerTests.cs</strong></p>
  
  <pre><code>[Theory]
      [MemberData(nameof(OrderControllerData.postCancelOrder), MemberType = typeof(OrderControllerData))]
      public async Task PostValidCancelOrder(string jsonBody, string expectedResponseBody)
      {
          var postObject = "api/Order/cancel";
          var request = new StringContent(jsonBody, Encoding.UTF8, "application/json");
          var response = await _httpClient.PostAsync(postObject, request);
          var content = await response.Content.ReadAsStringAsync();
  
          ValidationHelper.ValidateNoErrors(response);
          ValidationHelper.ValidateJson(content, expectedResponseBody);
      }
  </code></pre>
  
  <p>A few things are happening in this test. All of which seem to be working fine and as intended (except for maybe #3 below).</p>
  
  <ol>
  <li>Create my requestUri with postObject.</li>
  <li>Create my HttpContent. XUnit is passing my jsonBody param by way of the MemberData attribute. Which looks into my data layer class for the postCancelOrder public static IEnumerable.</li>
  <li>Posting the request to the API.</li>
  <li>Reading the payload returned back in string format.</li>
  <li>Validation Helper checks for HTTP response messages. The ValidateNoErrors method is simply just looking for a 200 response code. The second one doesn't matter for this question since my test blows up at the first ValidationHelper.</li>
  </ol>
  
  <p>Right now this is currently what I am posting to the api.</p>
  
  <pre><code>"{\"orderId\": \"c701b60a-4fb1-4f79-9e17-0172a9a17bbc\",\"test1Fee\":\"5}\",\"test2Fee\":\"10\"}"
  </code></pre>
  
  <p>Here is the HttpPost within the Api/OrderController.</p>
  
  <p><strong>OrderController.cs</strong></p>
  
  <pre><code>[HttpPost("cancel")]
      public async Task&lt;IActionResult&gt; Cancel([FromBody]CancelOrderViewModel model)
      {
          var command = new CancelOrderCommand(model.OrderId, model.Test1Fee, model.Test2Fee);
          var order = await _mediator.Send(command);
  
          return Ok(order);
      }
  </code></pre>
  
  <p>Essentially this HttpPost is passing a few parameters and then running a multiple methods and checks on those objects to determine if the order is eligible to be cancelled. What happens within the post doesn't matter right now. When debugging my test and it hits the entry point of the HttpPost method of the api, the 'command' and 'model' are both null. Naturally the Api will reject the request and responds with a 500 error.</p>
  
  <pre><code>{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.NoWriteNoSeekStreamContent, Headers:{  Date: Mon, 22 Jan 2018 19:38:55 GMT  Transfer-Encoding: chunked  Server: Kestrel  Request-Context: appId=cid-v1:93254013-d606-465f-8cee-12e422316f31  X-SourceFiles: =?UTF-8?B?RDpcUUFccWFWZW5kb3JcQWJzdHJhY3Rvck9tc0FwaVxzb3VyY2VcQXBpXGFwaVxPcmRlclxjYW5jZWw=?=  X-Powered-By: ASP.NET  Content-Type: application/json; charset=utf-8}}
  </code></pre>
  
  <p>Also the message is indicating that Object reference not set to an instance of an object since everything is null apparently.</p>
  
  <p>So the problem has to be with how I'm constructing the request within my XUnit test. But I'm confused because I believe I'm serializing the json correctly. Even when the Api is running locally in my browser using swagger, the indicated example is </p>
  
  <pre><code>{
    "orderId": "string",
    "test1Fee": 0,
    "test2Fee": 0
  }
  </code></pre>
  
  <p>Could the escapes being created within my post object be the problem? If so, how do I go about fixing that? I tried using PostAsJsonAsync() instead of just PostAsync() but it continues to yield the same results.</p>
  
  <p>What am I doing wrong here? I'm sure it's something pretty simple and I'm just missing it. Let me know if I need to provide anymore information. Any help would be greatly appreciated. Thanks.</p>