title: WebApi Serializing to Json with POST Request
body: <p>I have the following WebApi controller </p>
  
  <pre><code>[Route("api/[controller]")]
  public class FunctionController : ControllerBase
  {
      private readonly ILogger&lt;FunctionController&gt; _logger;
      private readonly IServiceAccessor&lt;IFunctionManagementService&gt; _functionManagementService;
  
      public FunctionController(
          IServiceAccessor&lt;IFunctionManagementService&gt; FunctionManagementService,
          ILogger&lt;FunctionController&gt; logger)
      {
          _functionManagementService = FunctionManagementService;
          _logger = logger;
      }
  
      [HttpPost]
      [SwaggerOperation(nameof(RegisterFunction))]
      [SwaggerResponse(StatusCodes.Status200OK, "OK", typeof(FunctionRegisteredResponseDto))]
      [SwaggerResponse(StatusCodes.Status400BadRequest, "Bad Request")]
      public async Task&lt;IActionResult&gt; RegisterFunction(RegisterFunctionDto rsd)
      {
          var registeredResponse = await _functionManagementService.Service.RegisterFunctionAsync(rsd);
          if (registeredResponse.Id &gt; -1)
              return Ok(registeredResponse);
  
          return BadRequest(registeredResponse);
      }
  
      [HttpDelete("{id}")]
      [SwaggerOperation(nameof(UnregisterFunction))]
      [SwaggerResponse(StatusCodes.Status200OK, "OK")]
      [SwaggerResponse(StatusCodes.Status404NotFound, "Not Found")]
      [SwaggerResponse(StatusCodes.Status400BadRequest, "Bad Request")]
      public async Task&lt;IActionResult&gt; UnregisterFunction(string sid)
      {
          if (!long.TryParse(sid, out long id))
              return new BadRequestObjectResult(new { message = "400 Bad Request", UnknownId = sid });
  
          if (!await _functionManagementService.Service.UnregisterFunctionAsync(id))
              return new NotFoundObjectResult(new { message = "404 Not Found", UnknownId = sid });
  
          return new OkObjectResult(new { Message = "200 OK", Id = id, Unregistered = true });
      }
  }
  </code></pre>
  
  <p>I am attempting to test requests to this service using MSTest. First I merely want to send a request to the service, I have attempted to do this (using <a href="https://forums.asp.net/t/2148414.aspx?Transfer+Objects+and+Data+from+Client+App+to+WebAPI" rel="nofollow noreferrer">this example</a>) via </p>
  
  <pre><code>[TestMethod]
  public async Task BuildObjectFromValidResponse()
  {
      RegisterFunctionDto rsd = Utils.GetRegisterFunctionDtoObject();
      string serializedDto = JsonConvert.SerializeObject(rsd);
  
      var inputMessage = new HttpRequestMessage()
      {
          Content = new StringContent(serializedDto, Encoding.UTF8, "application/json")
      };
      inputMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
      HttpResponseMessage response = await client.PostAsync("api/Function", inputMessage.Content);
  
      // Also tried this.
      //HttpResponseMessage response = await client.PostAsJsonAsync("api/Function", JsonConvert.SerializeObject(rsd));
  }
  
  public class RegisterFunctionDto
  {
      public string Name { get; set; }
      public decimal Movement { get; set; }
      public int Quantity { get; set; }
  }
  
  public static class Utils
  {
      private static Random random = new Random();
  
      public static string GetName(int length = 5)
      {
          StringBuilder resultStringBuilder = new StringBuilder();
          string dictionaryString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  
          for (int i = 0; i &lt; length; i++)
              resultStringBuilder.Append(dictionaryString[random.Next(dictionaryString.Length)]);
  
          return resultStringBuilder.ToString();
      }
  
      public static RegisterFunctionDto GetRegisterFunctionDtoObject()
      {
          return new RegisterFunctionDto()
          {
              Name = GetName(),
              Instruction = random.Next() % 2 == 0 ? BuySell.Buy : BuySell.Sell,
              PriceMovement = Convert.ToDecimal(random.NextDouble()),
              Quantity = 100
          };
      }
  }
  </code></pre>
  
  <p>But when I post this to the service, the received object is the default one, this is one with all default values. So in <code>RegisterFunction</code> I am receiving </p>
  
  <pre><code>rsd { Name = "", Movement = 0.0, Quantity = 0 }
  </code></pre>
  
  <p><em>Q. How can I correctly serialize my object using Newtonsoft.Json and post to me service?</em></p>