user1429595
user1429595

Reputation: 2735

How to Pass Dynamic Json Object (data) to wcf ResTful Services?

so Lets say I have a Json data set as following even though this json Data( the Model or structure) of this data is not static and it will change based on each call, How can I pass a generic Json data set into the POST method of WCF code?

{
  "experience": 14746,
  "status": true,
  "name": "Aaron",
  "uuid": "3123"
}

I want to use POSTMAN or SoapUI from the body?

public object PostData(string id, [FromBody] JObject data)
{
 //Do Something with data
}


public interface IPostService
{
    [OperationContract(Name = "PostData")]
    [WebInvoke(Method = "POST", UriTemplate = "/PostData?id={id}&data={data}")]
    object PostData(string id,[FromBody] JObject data);

}

any help would be appreciate it

Upvotes: 1

Views: 7776

Answers (2)

Jose Rodriguez
Jose Rodriguez

Reputation: 10152

Supplementing with my previous answer. Newtonsoft can be used with dynamic when deserialize an object. Is performed in this way.

var results = JsonConvert.DeserializeObject<dynamic>(json);
var experience= results.Experience;
var status= results.Status;
var name= results.Name;
var uuid= results.Uuid;
var dynamic_property= results.AnotherProperty;

Another way. If you know all possible properties of the class to parse. You can use a JsonProperty attribute of Newtonsoft.Json and use a DeserializeObject strong typed.

public class MyModel
{
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public int experience {get;set;}

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public bool status {get;set;}

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public string name {get;set;}

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public string uuid {get;set;}

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public object property_1 {get;set;}

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public object property_2 {get;set;}

    ...
}

You can make a request:

var httpRequest = WebRequest.Create(string.Format("baseurl" + "/PostData?id={0}", id));
httpRequest.Method = "POST";
httpRequest.ContentType = "application/json";
httpRequest.ContentLength = data.Length;

try
{
    using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
    {
        if (!string.IsNullOrEmpty(data))
        {
            streamWriter.Write(data);
            streamWriter.Flush();
            streamWriter.Close();
        }
    }

    var response = httpRequest.GetResponse();
}
catch (Exception)
{}

Upvotes: 1

Jose Rodriguez
Jose Rodriguez

Reputation: 10152

When you specify in UriTemplate attribute a variable data you are saying that value become in query string, not in BODY, by default a HTTP method of WebInvokeMethod attribute is POST.

public object PostData(string id, string data)
{
    //Do Something with data
}


public interface IPostService
{
    [OperationContract(Name = "PostData")]
    [WebInvoke(UriTemplate = "/PostData?id={id})]
    object PostData(string id, string data);
}

Then you can use a Newtonsoft library to parse a string value in format Json to a object with dynamic properties. You can use a Newtonsoft library with Nuget .

To learn, how parse dynamic objects with Newtonsoft, click here.

Upvotes: 1

Related Questions