aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

ASP.NET CORE Web API: Value are always null when doing HTTP Post requests.

Open ghost opened this issue 9 years ago • 46 comments

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 PostStock([FromBody]Stock model) { if (model != null) { var isStockAdded = await _inventorySvc.AddStockAsync(model);

            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?

ghost avatar Sep 18 '17 22:09 ghost

What does the Stock object look like? Also what are you sending to the endpoint?

jkotalik avatar Sep 18 '17 22:09 jkotalik

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"
}

ghost avatar Sep 18 '17 22:09 ghost

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?

jkotalik avatar Sep 18 '17 22:09 jkotalik

@lorenz31 I have tried like this and it does work. qq 20170919104338

zzyykk123456 avatar Sep 19 '17 02:09 zzyykk123456

@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" }

zzyykk123456 avatar Sep 19 '17 02:09 zzyykk123456

Thanks for checking that @zzyykk123456.

jkotalik avatar Sep 19 '17 02:09 jkotalik

@zzyykk123456 But how come the prices doesn't have the actual values being passed upon request?

ghost avatar Sep 19 '17 05:09 ghost

@lorenz31 It works too. 1 2

zzyykk123456 avatar Sep 19 '17 07:09 zzyykk123456

@zzyykk123456 I'm using Postman to test it but it doesn't pass the values even with or without the [FromBody] parameter attribute.

ghost avatar Sep 19 '17 07:09 ghost

@lorenz31 With or without [FromBody] is ok.The postman setup is here.

1 2

zzyykk123456 avatar Sep 19 '17 08:09 zzyykk123456

@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.

ghost avatar Sep 19 '17 08:09 ghost

@zzyykk123456 I created the asp.net core web api project via VS 2017 Community edition version 15.3.4.

ghost avatar Sep 19 '17 08:09 ghost

@zzyykk123456 This is just a class derived from DbContext.

ghost avatar Sep 19 '17 08:09 ghost

@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.

ghost avatar Sep 19 '17 08:09 ghost

@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?

ghost avatar Sep 19 '17 08:09 ghost

@zzyykk123456 I have the same setup as what you have.

ghost avatar Sep 19 '17 08:09 ghost

@zzyykk123456 Here's my setup

On Postman screenshot

On Visual Studio 2017 Community Edition screenshot

ghost avatar Sep 19 '17 11:09 ghost

@lorenz31 could you try camelCase for your property names in postman

kirst avatar Sep 19 '17 22:09 kirst

@kirst It still doesn't work.

ghost avatar Sep 20 '17 00:09 ghost

@lorenz31 Your StockId and SKU guid are invalid. You can get new guid in https://www.guidgenerator.com/online-guid-generator.aspx. Try it.

zzyykk123456 avatar Sep 20 '17 02:09 zzyykk123456