MattBH
MattBH

Reputation: 1642

Passing multiple scalar parameters to WebAPI via HttpClient.PostAsJsonAsync

Here's my method:

[HttpPost]
[ActionName("TestString")]
public string TestString([FromBody] int a, [FromBody] int b, [FromBody] int c)
{
  return "test " + a + " " + b + " " + c;
}

Is there any way that i can call this method using HttpClient.PostAsJsonAsync

I've tried this:

HttpResponseMessage response = client.PostAsJsonAsync("api/task/TestString","a=8,b=5,c=6").Result;

But I get this error: StatusCode: 500, ReasonPhrase: 'Internal Server Error'

Thanks in advance!

Upvotes: 2

Views: 2427

Answers (1)

Neil Thompson
Neil Thompson

Reputation: 6425

I'm pretty sure you are only allowed a single [FromBody] tag. Try (add your own error handling etc):

[HttpPost]
[ActionName("TestString")]
public string TestString([FromBody] dynamic body)
{
  return "test " + body.a.ToString() + " " + body.b.ToString() + " " + body.c.ToString();
}

This should work provided that for form body actually contains a,b and c.

Upvotes: 2

Related Questions