Home > Software design >  How to form RestSharp Request body for array of objects
How to form RestSharp Request body for array of objects

Time:12-22

I need to consume an endpoint that has JSON array of objects as the request structure. I have already tested it on a rest client. The problem is I am unable to form the request body in restsharp.

Below is the JSON structure

[
  {
    "id": "1",
    "name": "rejected",
    "timestamp": "2021-10-07T16:47:37Z",
    "identity": "MainId",
    "source": "web",
    "params": {
      "email": "[email protected]",
      "fullName": "John Doe",
      "Mobile": "444586867857"
    }
  }
]

I have also created the POCO class

    public class activityClass
    {
        public Class1[] Property1 { get; set; }
    }
    
    public class Class1
    {
        public string id { get; set; }
        public string name { get; set; }
        public DateTime timestamp { get; set; }
        public string identity { get; set; }
        public string source { get; set; }
        public Params _params { get; set; }
    }
    public class Params
    {
        public string email { get; set; }
        public string fullName { get; set; }
        public string Mobile { get; set; }
    }

There is the code to call the endpoint

var client = new RestClient("http://api.tech.com/apiv2");
           var request = new RestRequest(Method.POST);
           //ThIS IS WHERE THE PROBLEM IS
            var body = new activityClass
            {
                Class1 = new List<Class1>
                {
                }

            }
            var json = request.JsonSerializer.Serialize(body);
            request.AddParameter("application/json", json, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);

CodePudding user response:

I think You Should Use Serialize Object Like This:

request.AddParameter("application/json; charset=utf-8", JsonConvert.SerializeObject(body), ParameterType.RequestBody);

Also did you try to use request.AddJsonBody() ?

CodePudding user response:

The body you are creating is not supposed to be based on activityClass object... instead, based on your json, it should be List<Class1> object.

Your body should look like this instead,

var body = new List<Class1> {
  new Class1 {
    ... // this is where your properties are filled in.
  },
  new Class1 {
    ...
  }
}

Your implementation would actually work only if your json had a parent property, something like,

{
  "Property1": [
    { 
      "id": "1",
      "name": "rejected",
      ....
    }
  ]
}

And with RestSharp.. you create your request and use request.AddJsonBody(body) instead of adding a parameter. should look like this,

var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");

var body = new List<Class1> ...;
request.AddJsonBody(body);
IRestResponse response = client.Execute(request);

CodePudding user response:

I see a couple of things that would be causing issues for me if I was trying to construct this call myself.

  1. You are trying to serialize LINQ objects instead of the data contained within them
  2. The Anonymous type notation doesn't work well with non string items in my experience.
  3. If the JSON payload sample you provided is the goal it's not completely straightforward in structure as it has a seated JObject within the JArray entry.

I work from the center and go outwards when creating JSON payloads in code. I would start with the object that is the value for 'params.' It would look something like the below:

//create our innermost object and fill it
JObject params = new JObject();
params.Add("email", params.email)
params.Add("fullName", params.fullName)
params.Add("Mobile", params.Mobile)
//create the JSON Object that we insert that into and fill the other values
JObject outerBody = new JObject();
outerBody.Add("id", Class1.id)
outerBody.Add("name", Class1.name)
outerBody.Add("timestamp", Class1.timestamp)
outerBody.Add("identity", Class1.identity)
outerBody.Add("source", Class1.source)
outerBody.Add("params", params)
//now finally create the array and insert it
JArray ourArray = new JArray();
ourArray.Add(outerBody);

//once this object is done, you can add this in as a parameter coded as the body:
request.AddParameter("application/json", ourArray, ParemeterType.ReqeustBody);
  • Related