29

I have a basic post operation that works on a single object of RecordIem. What I would like to do is do the same action but in bulk by posting an array of requests using the same format.

For instance:

public HttpResponseMessage Post(RecordItem request)
{
    var recordItems = _recorder.RecordItem(request);
    return Request.CreateResponse(HttpStatusCode.OK, recordItems);
}

And when I post the Json:

{
    Id : "7UP24fVkGOxSjrcclghe_mP2-po",
    System : 1,
    Environment : "Production"
}

everything works fine. I would like to post Json similar to:

{
    Id : "7UP24fVkGOxSjrcclghe_mP2-po",
    System : 1,
    Environment : "Production"
},
{
    Id : "ClPE188H4TeD2LbQPeV_EzCsKVM",
    System : 1,
    Environment : "Production",
    Label : "RestTest1"
},
{
    Id : "SAWTMJzm-_AFqoNw70-gLeUzB4k",
    System : 1,
    Environment : "Production"
}

And have a method similar to below pick this up:

public HttpResponseMessage Post(RecordItem[] request)
{
    var recordItems = _recorder.RecordItems(request);
    return Request.CreateResponse(HttpStatusCode.OK, recordItems);
}

I've tried using both the [FromBody] and [ModelBinding] attributes on the array and tried using different types (List, IList, IEnumerable) but to no avail. When using [FromBody] the request parameter is null and when using [ModelBinding] the list is empty. I've tried using both and that doesn't work either.

I'd rather not have to having to resort to looping single posts in my client.

Thanks

3 Answers 3

24

For all that just get an empty array whatever they try, try this:

var request = $.ajax({
  dataType: "json",
  url: "/api/users",
  method: "POST",
  data: { '': postData}
});

The data must be a single anonymous object instead of a raw array.

Info was found here.

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

1 Comment

Still an issue as of today for me. Great fix!
21

Since your Post expects an RecordItem[], your JSON content in your request body should be in an array as well.

What you have is pretty close -- try adding a pair of square bracket [] around your data:

[{
    Id : "7UP24fVkGOxSjrcclghe_mP2-po",
    System : 1,
    Environment : "Production"
},
{
    Id : "ClPE188H4TeD2LbQPeV_EzCsKVM",
    System : 1,
    Environment : "Production",
    Label : "RestTest1"
},
{
    Id : "SAWTMJzm-_AFqoNw70-gLeUzB4k",
    System : 1,
    Environment : "Production"
}]

1 Comment

This still comes in with a count of 0, not sure how / why it worked for him but looks like this was from over 2 years ago...
2

It's important that your json contains the request parameter name. A other note: you have to post it as an array.

Your json would look like this:

{
    "request": [
        {
            "Id": "...",
            "System": 1,
            ...
        },
        { ... }
    ]
}

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.