ASP.NET CORE Web API: Value are always null when doing HTTP Post requests.
I'm experiencing a problem doing post requests via ASP.NET Core Web API. I'm a bit confused of the articles I read/found while Googling.
So here's my scenario:
I'm trying to do a HTTP Post request to an api endpoint with a complex parameter
Example:
[HttpPost]
[Route("add")]
public async Task
if (isStockAdded)
return Ok();
else
return BadRequest();
}
return BadRequest();
}
}
My problem is that every time I make a post request to this endpoint the model value(s) are always null even if there's the [FromBody] parameter attribute or not.
Is this a bug in ASP.NET Core 2?
What does the Stock object look like? Also what are you sending to the endpoint?
Thanks for responding @jkotalik
Here's the structure of Stock class
public class Stock
{
[Key]
public Guid StockId { get; set; }
[Required]
[DataType(DataType.Text)]
public string StockName { get; set; }
[Required]
[DataType(DataType.Text)]
public Guid SKU { get; set; }
public string Description { get; set; }
[Required]
public int Qty { get; set; }
[Required]
public DateTime DateAdded { get; set; }
[Required]
public decimal UnitPrice { get; set; }
[Required]
public decimal MarkupPrice { get; set; }
[Required]
public decimal SellingPrice { get; set; }
[ForeignKey("UserId")]
public virtual ApplicationUser User { get; set; }
[Required]
public Guid UserId { get; set; }
}
This is the data I'm sending to the endpoint using Postman:
{
"StockId":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"StockName":"test item",
"SKU":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Description":"test item",
"Qty":10,
"DateAdded":"XXXXXXXXXXX",
"UnitPrice":00,
"MarkupPrice":00,
"SellingPrice":00,
"UserId":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
}
Most likely one of these properties failed to bind. @rynowak I don't know who is in charge of model binding, but you have any ideas?
@lorenz31 I have tried like this and it does work.

@lorenz31 Stock like this: { "stockId": "8fc759e0-0da1-478d-96b7-d939e4498d48", "stockName": "string", "sku": "8fc759e0-0da1-478d-96b7-d939e4498d48", "description": "string", "qty": 0, "dateAdded": "2017-09-19T02:37:28.937Z", "unitPrice": 0, "markupPrice": 0, "sellingPrice": 0, "userId": "8fc759e0-0da1-478d-96b7-d939e4498d48" }
Thanks for checking that @zzyykk123456.
@zzyykk123456 But how come the prices doesn't have the actual values being passed upon request?
@lorenz31 It works too.

@zzyykk123456 I'm using Postman to test it but it doesn't pass the values even with or without the [FromBody] parameter attribute.
@lorenz31 With or without [FromBody] is ok.The postman setup is here.

@zzyykk123456 Hmm in VS 2017 I really can't get the values being passed to the endpoint upon the request. But as what you showed in your samples, this makes me confused.
@zzyykk123456 I created the asp.net core web api project via VS 2017 Community edition version 15.3.4.
@zzyykk123456 This is just a class derived from DbContext.
@zzyykk123456 Sorry, I was mistaken. Its just a class with 3 properties
public class ApplicationUser { [Key] public Guid Id { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
public virtual ICollection<Stock> Stocks { get; set; }
}
NOTE: I'm not using Asp.Net Identity here.
@zzyykk123456 Hmm so what do you think is the problem? How come that it can't receive the values being passed from postman to the api controller when making the POST request?
@zzyykk123456 I have the same setup as what you have.
@zzyykk123456 Here's my setup
On Postman
On Visual Studio 2017 Community Edition
@lorenz31 could you try camelCase for your property names in postman
@kirst It still doesn't work.
@lorenz31 Your StockId and SKU guid are invalid. You can get new guid in https://www.guidgenerator.com/online-guid-generator.aspx. Try it.