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?

1
  • How would the problematic "client request" would look like? Commented Aug 20, 2015 at 8:22

3 Answers 3

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.

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

2 Comments

Maybe I should be more specific. What I'm looking for is key = [10,20,30,40,50]. The value is the array.
ah ok, I was under the impression there were keys for each value! edit coming soon.....
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[]

Comments

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

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.