I try to retrieve the model i sent as jSon using restSharp to my API controller, but the TestCard object is always null when I receive it in my controller, why is that?
Model
public class TestCard
{
public int OrderId { get; set; }
public List<Person> Persons { get; set; }
}
public class Person
{
public string Name { get; set; }
public string Adress { get; set; }
public string Adress2 { get; set; }
public string PostalCode { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
Test Post
var personList = new List<Person>();
personList.Add(new Person
{
Name = "John Doe",
Adress = "West Park Ave",
PostalCode = "123",
City = "NY",
Country = "US"
});
personList.Add(new Person
{
Name = "Sandra Doe",
Adress = "West Park Ave",
PostalCode = "123",
City = "NY",
Country = "US"
});
var order = new TestCard() { OrderId = 1, Persons = personList };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var client = new RestClient("https://localhost:44336/api/card/");
client.Authenticator = new HttpBasicAuthenticator("myusername", "mypassword");
var request = new RestRequest("Add", Method.Post);
request.AddBody(JsonConvert.SerializeObject(order));
request.AddHeader("Content-Type", "application/json");
Task<RestResponse> response = client.ExecuteAsync(request);
if (response.Result.StatusCode == HttpStatusCode.Created)
{
Response.Write("OK!");
}
Controller
[Route("api/Card/Add")]
[AcceptVerbs("GET", "POST")]
[HttpPost]
public IHttpActionResult Add([FromBody] TestCard model)
{
return Ok();
}
CodePudding user response:
when you use request.addbody you don't have to serialize object
request.AddBody(order);
and IMHO , remove [AcceptVerbs("GET", "POST")] since you have already [HttpPost]