0

I am developing a WebApi endpoint for a client application. The API has a method with the following signature:

public async Task<IHttpActionResult> Post([FromBody] int[] employeeIds)

The sample POST-request I use to trigger this endpoint:

Authorization: Bearer [token]
Host: localhost:44301
Content-Length: 16
Content-Type: application/json

[10,20,30,40,50]

This all works fine and dandy. The problem is that the client application can only send key/value pairs. As you can see from the sample POST-request, I am only sending the value of the array.

The only solution I can think of is to define a model with the array as a property, but this adds a new class to my codebase with no other purpose than being a container.

How do I overcome this problem?


Edit

I'm looking for a solution that allows the client to send the array as the value component of a key/value pair:

key = [10, 20, 30, 40, 50]

How can I transform my method into accepting such a request?

transporter_room_3
  • 2,583
  • 4
  • 33
  • 51

3 Answers3

1

You could use a Dictionary like so:

public async Task<IHttpActionResult> Post([FromBody] Dictionary<string, int> employeeIds)

Then you could send a request like the following

Authorization: Bearer [token]
Host: localhost:44301
Content-Length: 16
Content-Type: application/json

{
    'first': 10,
    'second': 20,
    'third': 30
}

****EDIT****

In response to your update and comment on my answer, in that there is one key per array of numbers. You could use the .Net type Tuple like this:

public async Task<IHttpActionResult> Post([FromBody] Tuple<string, int[]> employeeIds)

you then access the values by code using

string myKey = employeeIds.Item1;
int[] theIds = employeeIds.Item2;

The request looks like this:

Authorization: Bearer [token]
Host: localhost:44301
Content-Length: 16
Content-Type: application/json

{
   Item1: 'OfficeWorkers',
   Item2: [10, 20, 30, 40, 50]
}

However this is obviously less declarative and the tuple is less obvious to others who use the api/modify your code in the future.

If it was up to me, I'd favour a small class (only existing in the API layer) which acts as a simple model which holds data received from the API calls - then in the WebApi code - map that to a "proper" class you are using internally.

ry8806
  • 2,258
  • 1
  • 23
  • 32
0

Web API supports parsing content data in a variety of ways, but it does not deal with multiple posted content values. A solution for your problem could be to create a ViewModel with a property of int[] type.

Here you can read more about this question: Passing array of integers to webapi Method

Posting array of integers to asp.net web api

In short you need create ViewModel with property of int[]

Community
  • 1
  • 1
David Abaev
  • 690
  • 5
  • 22
0

If I think your issue right, here is my sample code:

public string Post([FromBody]Dictionary<string, int[]> value)
{
      return "OK";
}

Postman client:

enter image description here

Hope this helps!

enter image description here

BNK
  • 23,994
  • 8
  • 77
  • 87