Kgn-web
Kgn-web

Reputation: 7555

Why getting null object

I am testing my web api via postman. But getting the passed request object as null.

Below is WebAPI action.

    [AcceptVerbs("POST")]
    [HttpPost]
    [Route("rename")]
    public IHttpActionResult Rename([FromBody]Customer custObj)
    {

        //I am getting custObj always null
    }

Initially, I got the below error.

Error sending json in POST to web API service

Using the below link, I came to know that I missed to set the content-type:application/json
Error sending json in POST to web API service

After setting the content type, the API method is hit but I am getting the customer object as null.

Postman

public class Customer
{
   public int UserId {get; set;}

   public string Title {get; set;}


  //& so on
}

Any help/suggestion highly appreciated. Thanks

Upvotes: 1

Views: 381

Answers (2)

afrose
afrose

Reputation: 169

Try sending it as

"customer": {
  "userid": 1,
  "title": "something"
}

and include this as json

Upvotes: 0

Dante May Code
Dante May Code

Reputation: 11247

In POSTMAN,

Select "raw" instead of "form-data"

Select "JSON (application/json)" instead of (default) "Text"

Paste

{
    "UserId": 1,
    "Type": "Gold",
    "Title": "Test rename api via postman"
}

Click "Send"

Upvotes: 4

Related Questions