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.










share|improve this question





















  • 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














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.










share|improve this question





















  • 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












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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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












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;




screenshot on the breackpoint



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]






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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;




    screenshot on the breackpoint



    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]






    share|improve this answer


























      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;




      screenshot on the breackpoint



      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]






      share|improve this answer
























        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;




        screenshot on the breackpoint



        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]






        share|improve this answer














        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;




        screenshot on the breackpoint



        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]







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 9 at 15:54

























        answered Nov 9 at 15:33









        rmjoia

        646717




        646717



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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














































































            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20