Search code examples
c#asp.netpostasp.net-web-apipostman

Why are no parameters passed in when I try to test my API with Postman?


I've built a simple WEB API using Microsoft Visual Studio 2012 MVC 4 ASP.NET C#. I'm trying to test if my POST Request method works.

My Post method takes an Object as a parameter.

 public class Object
    {
        [Key]
        public int ID{ get; set; }
        public string Name { get; set; }
        

        public Object()
        {
            
        }
        public Object(int i, string n)
        {
            ID = i;
            Name = n;
        }
    }
}

Inside my ObjectController I have this:

// POST api/Object
        public HttpResponseMessage PostObject (Object o)
        {
            if (o == null)
                throw new NullReferenceException("Nothing is being passed in");

            return HelperFunction.Post(o);
            //HelperFunction class not shown. Post method is static.
        }

Now when I try to use Postman to test this, I keep getting the Null Reference Exception I made that Object o is null

For key, I am literally putting the letter o For value, I have tried the following:

  1. new Object (3,"Test")
  2. (3, "test")
  3. I even tried changing the whole code such that my Post Method takes 2 parameters of int and string and then makes its own Object.

I think this is merely a question of Postman syntax, but in the case that it is more, I have put all my code.

Edit: My class name nor my controller is not actually called "Object". This might have caused some confusion as this is a reserved word. I was trying to make the code more readable. Let me know if this is confusing and I will edit it.


Solution

  • Without knowing what your request body looks like and how exactly you have postman it's hard to really know exactly what you are missing. Based on what I could get from your question I created a screenshot of postman and marked a few places for you to double check.

    Make sure you are indeed sending a POST request. Seems likely as it sounds like your action method is found.

    Make sure the URL is correct. Same as above, seems likely as it sounds like your action method is found.

    Ensure your body is valid JSON and that it is sent as JSON. That is set content type to application/json.

    As Sandeep Kushwah noted it may be a good idea to inspect the actual request through Fiddler as well.

    postman snapshot