Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting dictionary to web api in asp.net core

I have simple web api developed using Asp.Net Core and I am trying to post key-value pair using HttpClient. I tried two approaches.

1st Approach

[Route("api/[controller]/[action]")]
public class TransformationController : Controller
{
    private IServiceResolver _serviceResolver;
    public TransformationController(IServiceResolver serviceResolver)
    {
        _serviceResolver = serviceResolver;
    }       

    [HttpPost]
    [ActionName("Post1")]
    public void Post1([FromBody]Dictionary<string, string> data)
    {
       // do something
    }
}

and then I am posting it as below

    [Fact]
    public void TestPost1()
    {
        var content = new Dictionary<string, string>();
        content.Add("firstname", "foo");            
        var httpContent = new FormUrlEncodedContent(content);

        var client = new HttpClient();
        var result = client.PostAsync("http://localhost:17232/api/Transformation/Post1", httpContent).GetAwaiter().GetResult();
    }

but I am getting Unsupported Media Type error

{StatusCode: 415, ReasonPhrase: 'Unsupported Media Type', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Date: Mon, 29 Aug 2016 19:44:44 GMT Server: Kestrel X-SourceFiles: =?UTF-8?B?QzpccmVwb3NcY3ItbWV0YXRhc2tlclxzcmNcSW5ib3VuZEludGVncmF0aW9uXFRyYW5zZm9ybWF0aW9ucy5BcGlcYXBpXFRyYW5zZm9ybWF0aW9uXFRyYW5zZm9ybWF0aW9uMQ==?= X-Powered-By: ASP.NET Content-Length: 0 }}

Approach 2
Since I cannot specify content type and encoding in FormUrlEncodedContent I changed the signature of the post method, and now it take Json string as parameter. Idea is to de-serialize the string into dictionary.

    [HttpPost]
    [ActionName("Post2")]
    public void Post2([FromBody]string data)
    {
        var dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(data);
     // do something here

     }

and then I am posting the string as below

    [Fact]
    public void TestPost2()
    {   
        var httpContent = new StringContent("{ \"firstName\": \"foo\" }", Encoding.UTF8, "application/json");

        var client = new HttpClient();
        var result = client.PostAsync("http://localhost:17232/api/Transformation/Post2", httpContent).GetAwaiter().GetResult();
    }

however when I debug the test; the data parameter in Post2 method is null.

I am not sure what I'm missing here in both approaches? Can someone please help

Update1
If I use POSTMAN to post data then its working. So for Approach 1 I can post raw data as

{
  "firstname": "foo"
}

and Approach2 post raw data as

 "{\"firstname\": \"foo\"}"

howevere the same data is not working when I use HttpClient

like image 886
LP13 Avatar asked Jan 06 '23 11:01

LP13


1 Answers

Try to merge two approach:

[Fact]
public void TestPost3()
{   
    var httpContent = new StringContent("{ \"firstName\": \"foo\" }", Encoding.UTF8, "application/json");

    var client = new HttpClient();
    var result = client.PostAsync("http://localhost:17232/api/Transformation/Post3", httpContent).GetAwaiter().GetResult();
}

[HttpPost]
[ActionName("Post3")]
public void Post3([FromBody]IDictionary<string, string> data)
{
   // do something
}
like image 151
adem caglin Avatar answered Jan 14 '23 01:01

adem caglin