0

I have below code written to test for reading the dictionary key and values from Postman.

C# web api method:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Service/testdictionary",
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void testdictionary(Dictionary<string,object> data)
{
if(data != null)
{
string str1 = data["a1"].ToString();
string str2 = data["a2"].ToString();
string str3 = data["a3"].ToString();
}
}

Postman input Body:(raw and JSON(application/json))

{
"data": {
"a1": "b1",
"a2": "b2",
"a3": "b3"
}
}

How am I calling the method from Postman:

Postman Call

What is the issue: Whenever I am trying to assign dictionary object data from the postman, in C# code it is getting assigned as an empty dictionary.

What is required: I want to read a dictionary element from postman to my API code.

Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84

4 Answers4

2

Your items are not data["a1"], data["a2"], etc. You have a dictionary of ONE item whose key is "data", so it is data["data"]. Your value for that one item is an object with a1, a2 and a3 properties. Not sure what that works out to in your WCF app but in Web API it is a JToken from the Newtonsoft Json.Net library.

If you want a Dictionary with keys a1, a2, a3 you need to adjust your JSON body to exclude the "data" level:

{
"a1": "b1",
"a2": "b2",
"a3": "b3"
}

UPDATE

If you are unable to change the JSON structure for some reason, you could create a class with a data property that is of Type Dictionary<string,object>:

public class TheData
{
    public Dictionary<string, object> data {get;set;}
}

Then your api method would take a TheData type instead of Dictionary<string,object>:

public void testdictionary(TheData _data)
{
    ...

and your code would access the dictionary like:

string d1 = _data.data["a1"].ToString(); // d1 == "b1" 
Crowcoder
  • 11,250
  • 3
  • 36
  • 45
  • Thanks for Sharing the Solution unfortunately the solution didnt work for dictionary. Not sure whether its a limitation for WCF service. But I have managed to get idea from your solution. Posting my Solution below. – Pratik Navagekar Dec 23 '18 at 17:39
0

Try this.

i tried! it works fine.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace StackOverFlowProblemWebApi.Controllers
{
    public class TestController : ApiController
    {
        [HttpPost]
        public IHttpActionResult TestDictionary([FromBody]Dictionary<string,object> data)
        {
            if (data != null)
            {
                string string1 = data["a1"].ToString();
                string string2 = data["a2"].ToString();
                string string3 = data["a3"].ToString();

                return Ok("Data Recieved."); // When the data is successfully recieved.
            }
            else
                return BadRequest("Data is not received.");
        }
    }
}

enter image description here

Look! did you see that: the data is received. ...

Rehan Shah
  • 1,505
  • 12
  • 28
0

Managed to solve my problem as a workaround, can say similar kind of dictionary.

What's the Solution: Use class object instead of dictionary.

Here is My Solution:

I add a class, similar structure as dictionary I wanted.(Only thing is I have to code extra line of code to declare class with Keys , which i was avoiding earlier)

Here is my code from web api:

    public class mydictionary
    {
        public string a1 { get; set; }
        public string a2 { get; set; }
        public string a3 { get; set; }
    }

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "Service/testdictionary",
    BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string testdictionary(mydictionary data)
    {
        if (data != null)
        {
            string str1 = data.a1.ToString();
            string str2 = data.a2.ToString();
            string str3 = data.a3.ToString();
            return "success".ToString();
        }
       else
        {
            return "unsuccess".ToString();
        }

    }

Here is my Postman input and Output: Postman Screen Shot

0

In my case, it worked in this format ActionParam = [{ Key: 'orderId', Value: '200' }]; 'ActionParam' is a dictionary property in a model and also tested on WCF.

ni3.net
  • 377
  • 4
  • 14