Skip to content

Instantly share code, notes, and snippets.

@yetanotherchris
Created September 29, 2017 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yetanotherchris/ecc7ca6b729ce476f0e9b526dd22ca46 to your computer and use it in GitHub Desktop.
Save yetanotherchris/ecc7ca6b729ce476f0e9b526dd22ca46 to your computer and use it in GitHub Desktop.
Postman and ASP.NET MVC POST returns null

If you are using Postman or manually calling your ASP.NET MVC REST api, and getting a null coming through this could be for various reasons:

[Route("Post")]
[HttpPost)
public string Post([FromBody] MyModel model)
{
   // do something with model
}

public class MyModel
{
    public Guid? Id { get;set; }
}

// Posted JSON:
{
    "Id" : ""
}
  • [FromBody] is required for HttpPost
  • This also enables Swagger (NSwag) calls to be tried out correctly, however it sees everything as a string.
  • NSwag needs [Route] attributes to recognise your method
  • NSwag errors can be found out by looking at its JSON file.
  • Postman needs Content-Type: application/json and a Raw body
  • Your model properties need to be nullable if they are optional
  • Your model needs a parameterless constructor.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment