2

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.

7
  • Related: Is a list/array valid JSON? Commented Nov 9, 2018 at 12:33
  • Can you give us an MCVE please Commented Nov 9, 2018 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. Commented Nov 9, 2018 at 12:56
  • Yeah the JSON validates fine. Commented Nov 9, 2018 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. Commented Nov 9, 2018 at 14:12

1 Answer 1

1

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]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.