What is the correct way to POST arrays (Lists) of objects in .NET Core 2?
up vote
2
down vote
favorite
I have an API method with this signature:
public async Task<IActionResult> PostCompanies([FromBody] List<Company> companies)
...and the auto generated swagger docs show that the JSON should be a plain array:
[
"fuelSiteId":228972,
"name": "foo"
,
"fuelSiteId":300000010,
"name": "bar"
]
However, if I post this back, it doesn't work and I get the error:
"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Company' because the type requires a JSON object"
The fix is to add the array to a dummy property:
data: [
"fuelSiteId":228972,
"name": "foo"
,
"fuelSiteId":300000010,
"name": "bar"
]
But everything I'm finding on this site implies this shouldn't be necessary and isn't how you'd normally do it in REST. Additionally, it means what the auto-generated docs say isn't actually what should be posted!
Which is wrong? My API code or the auto-gen Swagger docs or something else?
Ideally I would prefer my API to accept the 'plain' version as this seems more standard and more natural.
.net json rest .net-core
|
show 1 more comment
up vote
2
down vote
favorite
I have an API method with this signature:
public async Task<IActionResult> PostCompanies([FromBody] List<Company> companies)
...and the auto generated swagger docs show that the JSON should be a plain array:
[
"fuelSiteId":228972,
"name": "foo"
,
"fuelSiteId":300000010,
"name": "bar"
]
However, if I post this back, it doesn't work and I get the error:
"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Company' because the type requires a JSON object"
The fix is to add the array to a dummy property:
data: [
"fuelSiteId":228972,
"name": "foo"
,
"fuelSiteId":300000010,
"name": "bar"
]
But everything I'm finding on this site implies this shouldn't be necessary and isn't how you'd normally do it in REST. Additionally, it means what the auto-generated docs say isn't actually what should be posted!
Which is wrong? My API code or the auto-gen Swagger docs or something else?
Ideally I would prefer my API to accept the 'plain' version as this seems more standard and more natural.
.net json rest .net-core
Related: Is a list/array valid JSON?
– Peter B
Nov 9 at 12:33
Can you give us an MCVE please
– ADyson
Nov 9 at 12:37
The correct parameter for your method should be a type that takes an object with a int property and a string property.. FromBody is to handle the post of form data. so if you remove the [FromBody] maybe it works already? The Company type has to have a FuelSiteID (int) and Name (string) property though.
– rmjoia
Nov 9 at 12:56
Yeah the JSON validates fine.
– NickG
Nov 9 at 14:12
@rmjoia My real object has a couple of dozen properties, but I've tried removing FromBody and it doesn't fix the problem. It still needs it assigned to a dummy property to work.
– NickG
Nov 9 at 14:12
|
show 1 more comment
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have an API method with this signature:
public async Task<IActionResult> PostCompanies([FromBody] List<Company> companies)
...and the auto generated swagger docs show that the JSON should be a plain array:
[
"fuelSiteId":228972,
"name": "foo"
,
"fuelSiteId":300000010,
"name": "bar"
]
However, if I post this back, it doesn't work and I get the error:
"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Company' because the type requires a JSON object"
The fix is to add the array to a dummy property:
data: [
"fuelSiteId":228972,
"name": "foo"
,
"fuelSiteId":300000010,
"name": "bar"
]
But everything I'm finding on this site implies this shouldn't be necessary and isn't how you'd normally do it in REST. Additionally, it means what the auto-generated docs say isn't actually what should be posted!
Which is wrong? My API code or the auto-gen Swagger docs or something else?
Ideally I would prefer my API to accept the 'plain' version as this seems more standard and more natural.
.net json rest .net-core
I have an API method with this signature:
public async Task<IActionResult> PostCompanies([FromBody] List<Company> companies)
...and the auto generated swagger docs show that the JSON should be a plain array:
[
"fuelSiteId":228972,
"name": "foo"
,
"fuelSiteId":300000010,
"name": "bar"
]
However, if I post this back, it doesn't work and I get the error:
"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Company' because the type requires a JSON object"
The fix is to add the array to a dummy property:
data: [
"fuelSiteId":228972,
"name": "foo"
,
"fuelSiteId":300000010,
"name": "bar"
]
But everything I'm finding on this site implies this shouldn't be necessary and isn't how you'd normally do it in REST. Additionally, it means what the auto-generated docs say isn't actually what should be posted!
Which is wrong? My API code or the auto-gen Swagger docs or something else?
Ideally I would prefer my API to accept the 'plain' version as this seems more standard and more natural.
.net json rest .net-core
.net json rest .net-core
asked Nov 9 at 12:26
NickG
4,223114889
4,223114889
Related: Is a list/array valid JSON?
– Peter B
Nov 9 at 12:33
Can you give us an MCVE please
– ADyson
Nov 9 at 12:37
The correct parameter for your method should be a type that takes an object with a int property and a string property.. FromBody is to handle the post of form data. so if you remove the [FromBody] maybe it works already? The Company type has to have a FuelSiteID (int) and Name (string) property though.
– rmjoia
Nov 9 at 12:56
Yeah the JSON validates fine.
– NickG
Nov 9 at 14:12
@rmjoia My real object has a couple of dozen properties, but I've tried removing FromBody and it doesn't fix the problem. It still needs it assigned to a dummy property to work.
– NickG
Nov 9 at 14:12
|
show 1 more comment
Related: Is a list/array valid JSON?
– Peter B
Nov 9 at 12:33
Can you give us an MCVE please
– ADyson
Nov 9 at 12:37
The correct parameter for your method should be a type that takes an object with a int property and a string property.. FromBody is to handle the post of form data. so if you remove the [FromBody] maybe it works already? The Company type has to have a FuelSiteID (int) and Name (string) property though.
– rmjoia
Nov 9 at 12:56
Yeah the JSON validates fine.
– NickG
Nov 9 at 14:12
@rmjoia My real object has a couple of dozen properties, but I've tried removing FromBody and it doesn't fix the problem. It still needs it assigned to a dummy property to work.
– NickG
Nov 9 at 14:12
Related: Is a list/array valid JSON?
– Peter B
Nov 9 at 12:33
Related: Is a list/array valid JSON?
– Peter B
Nov 9 at 12:33
Can you give us an MCVE please
– ADyson
Nov 9 at 12:37
Can you give us an MCVE please
– ADyson
Nov 9 at 12:37
The correct parameter for your method should be a type that takes an object with a int property and a string property.. FromBody is to handle the post of form data. so if you remove the [FromBody] maybe it works already? The Company type has to have a FuelSiteID (int) and Name (string) property though.
– rmjoia
Nov 9 at 12:56
The correct parameter for your method should be a type that takes an object with a int property and a string property.. FromBody is to handle the post of form data. so if you remove the [FromBody] maybe it works already? The Company type has to have a FuelSiteID (int) and Name (string) property though.
– rmjoia
Nov 9 at 12:56
Yeah the JSON validates fine.
– NickG
Nov 9 at 14:12
Yeah the JSON validates fine.
– NickG
Nov 9 at 14:12
@rmjoia My real object has a couple of dozen properties, but I've tried removing FromBody and it doesn't fix the problem. It still needs it assigned to a dummy property to work.
– NickG
Nov 9 at 14:12
@rmjoia My real object has a couple of dozen properties, but I've tried removing FromBody and it doesn't fix the problem. It still needs it assigned to a dummy property to work.
– NickG
Nov 9 at 14:12
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
This also works without having to wrap in another type, give it a try.
I've tested with Postman
[HttpPost]
public JsonResult InsertPatientAppointment([FromBody] List<Company> companies)
return new JsonResult(companies.Select(c =>
new
c.FuelSiteId,
c.Name,
));
public class Company
[JsonProperty("fuelSiteId")]
public int FuelSiteId
get;
set;
[JsonProperty("name")]
public string Name
get;
set;
You can also check other resource on SO how to post json object array to a web api I had to dig a bit myself as I didn't remember anymore how to do this..
As stated at Parameter Binding in ASP.NET Web API: "To force Web API to read a complex type from the URI, add the [FromUri] attribute to the parameter."
Using [FromBody]
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
This also works without having to wrap in another type, give it a try.
I've tested with Postman
[HttpPost]
public JsonResult InsertPatientAppointment([FromBody] List<Company> companies)
return new JsonResult(companies.Select(c =>
new
c.FuelSiteId,
c.Name,
));
public class Company
[JsonProperty("fuelSiteId")]
public int FuelSiteId
get;
set;
[JsonProperty("name")]
public string Name
get;
set;
You can also check other resource on SO how to post json object array to a web api I had to dig a bit myself as I didn't remember anymore how to do this..
As stated at Parameter Binding in ASP.NET Web API: "To force Web API to read a complex type from the URI, add the [FromUri] attribute to the parameter."
Using [FromBody]
add a comment |
up vote
0
down vote
This also works without having to wrap in another type, give it a try.
I've tested with Postman
[HttpPost]
public JsonResult InsertPatientAppointment([FromBody] List<Company> companies)
return new JsonResult(companies.Select(c =>
new
c.FuelSiteId,
c.Name,
));
public class Company
[JsonProperty("fuelSiteId")]
public int FuelSiteId
get;
set;
[JsonProperty("name")]
public string Name
get;
set;
You can also check other resource on SO how to post json object array to a web api I had to dig a bit myself as I didn't remember anymore how to do this..
As stated at Parameter Binding in ASP.NET Web API: "To force Web API to read a complex type from the URI, add the [FromUri] attribute to the parameter."
Using [FromBody]
add a comment |
up vote
0
down vote
up vote
0
down vote
This also works without having to wrap in another type, give it a try.
I've tested with Postman
[HttpPost]
public JsonResult InsertPatientAppointment([FromBody] List<Company> companies)
return new JsonResult(companies.Select(c =>
new
c.FuelSiteId,
c.Name,
));
public class Company
[JsonProperty("fuelSiteId")]
public int FuelSiteId
get;
set;
[JsonProperty("name")]
public string Name
get;
set;
You can also check other resource on SO how to post json object array to a web api I had to dig a bit myself as I didn't remember anymore how to do this..
As stated at Parameter Binding in ASP.NET Web API: "To force Web API to read a complex type from the URI, add the [FromUri] attribute to the parameter."
Using [FromBody]
This also works without having to wrap in another type, give it a try.
I've tested with Postman
[HttpPost]
public JsonResult InsertPatientAppointment([FromBody] List<Company> companies)
return new JsonResult(companies.Select(c =>
new
c.FuelSiteId,
c.Name,
));
public class Company
[JsonProperty("fuelSiteId")]
public int FuelSiteId
get;
set;
[JsonProperty("name")]
public string Name
get;
set;
You can also check other resource on SO how to post json object array to a web api I had to dig a bit myself as I didn't remember anymore how to do this..
As stated at Parameter Binding in ASP.NET Web API: "To force Web API to read a complex type from the URI, add the [FromUri] attribute to the parameter."
Using [FromBody]
edited Nov 9 at 15:54
answered Nov 9 at 15:33
rmjoia
646717
646717
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225710%2fwhat-is-the-correct-way-to-post-arrays-lists-of-objects-in-net-core-2%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Related: Is a list/array valid JSON?
– Peter B
Nov 9 at 12:33
Can you give us an MCVE please
– ADyson
Nov 9 at 12:37
The correct parameter for your method should be a type that takes an object with a int property and a string property.. FromBody is to handle the post of form data. so if you remove the [FromBody] maybe it works already? The Company type has to have a FuelSiteID (int) and Name (string) property though.
– rmjoia
Nov 9 at 12:56
Yeah the JSON validates fine.
– NickG
Nov 9 at 14:12
@rmjoia My real object has a couple of dozen properties, but I've tried removing FromBody and it doesn't fix the problem. It still needs it assigned to a dummy property to work.
– NickG
Nov 9 at 14:12