0

I've been trying to send data to an API in order for the API to persist an operation in a database. I couldn't do it and I can't find the solution to it. Please help.

Model Dto:

public class NewRentalDto
{
    public int CustomerId { get; set; }
    public List<int> MovieIds { get; set; }
}

Controller definition:

public IHttpActionResult NewRentals(NewRentalDto newRental){
   //todo
}

Json objects sent through Postman:

Attempt 1:

{
 "movieIds": [3,4]
 ,"customerId": 1004
}

Result: Exception

Attempt 2:

{
 "MovieIds": [3,4]
 ,"CustomerId": 1004
}

Result: Exception

Attempt 3:

{
 "Movieids": [3,4]
 ,"Customerid": 1004
}

Result: Exception

Attempt 5:

{    
 "customerId": 1004
,"movieIds": [3,4]
}

Result: Exception

I get the exception in the "todo" section of the controller when I try to access the newRental instance. This is the message I get:

{
       "message": "An error has occurred.",
       "exceptionMessage": "Object reference not set to an instance of an object."
       //more error info
}
Erik Philips
  • 48,663
  • 7
  • 112
  • 142
Ivan Derlich
  • 439
  • 4
  • 14
  • Most probably the way you pass the data from the client is not correct I think that's why newRental is null. So you are getting null reference exception – Code-EZ Oct 14 '18 at 23:59
  • How you are passing data from postman? can u attach a screenshot – Code-EZ Oct 15 '18 at 00:00
  • You need to show what options you are setting (all options you have shown will work if you have set them correctly, and the method is a `POST` –  Oct 15 '18 at 00:10
  • The problem wasn't a model binding problem. It was that I wasn't initializing the data context variable inside the controller. @StephenMuecke – Ivan Derlich Oct 15 '18 at 18:15
  • @JameelM Thank you for your comment. The problem wasn't a model binding problem. It was that I wasn't initializing the data context variable inside the controller – Ivan Derlich Oct 15 '18 at 18:16

2 Answers2

0

Have you defined the verb that you are using when sending the object? e.g. the API is expecting a [HttpPost] or [HttpGet] ..

When I define an API endpoint I usually set the verb explicitly and where it is coming from.

e.g.

[HttpPost]
public IHttpActionResult NewRentals([FromBody]NewRentalDto newRental)

This will line up with the options you have chosen in Postman. If the verb is incorrect (e.g. the controller expects a "GET" and you are "POST"ing the data) then the instance of the object will be null.

Also. You may want to include Newtonsoft as a reference in your object and tag the properties as shown below:

using Newtonsoft.Json;

[JsonObject]
public class NewRentalDto
{
    [JsonProperty]
    public int CustomerId { get; set; }

    [JsonProperty]
    public List<int> MovieIds { get; set; }
}
Joe Walters
  • 431
  • 4
  • 6
0

Are you Missing Headers "Content-Type" to pass your sample is working perfect

// POST: api/Default
public void Post([FromBody]NewRentalDto newRentalDto)
{

}

Model

public class NewRentalDto
{
    public int CustomerId { get; set; }
    public List<int> MovieIds { get; set; }
}

Posting Request

enter image description here

Debug View

enter image description here

Saineshwar
  • 3,151
  • 5
  • 37
  • 41
  • Thank you for the answer. That part of the header was not missing at the moment I've asked the question – Ivan Derlich Oct 15 '18 at 17:29
  • @IvanDerlich what was issue may i know. – Saineshwar Oct 15 '18 at 17:41
  • The issue was that I wasn't initializing the data context I was just declaring it. And I when I referenced it inside the action of the controller, I got a null instance. It was not a problem of model binding. – Ivan Derlich Oct 15 '18 at 18:10