CodingCode
CodingCode

Reputation: 123

How to post a JSON Array of Objects in RestSharp

I need to pass a JSON Array of objects, here's an example of what it should look like in JSON:

   "categories": [
    {
      "id": 9
    },
    {
      "id": 14
    }
  ],

I can't figure out how to get it done by myself, I tried using Restsharp's request.AddBody() and request.AddParameter() but it didn't get me anywhere =/

var request = new RestRequest();
request.AddParameter("name", name);
// Category
request.AddParameter("categories", "categories here");
var response = client.Post(request);

Upvotes: 1

Views: 9231

Answers (3)

Herbert
Herbert

Reputation: 85

Create a class and give it any name

class MyObject
{
   private int id;
   public MyObject(int id)
   {
     this.id = id;
   }
}

Define your class as an object

MyObject obj = new MyObject(9);

Now using Newtonsoft.Json serialize your object

string result = JsonConvert.SerializeObject(obj);

Now add it to an array

var resArray = new object[] { result };

Find the Complete code below

    class MyObject
    {
       private int id;
       public MyObject(int id)
       {
         this.id = id;
       }
    }


using Newtonsoft.Json;
using RestSharp;
class Main
{
    MyObject obj = new MyObject(9);
    MyObject obj1 = new MyObject(14);
    string result = JsonConvert.SerializeObject(obj);
    string result1 = JsonConvert.SerializeObject(obj1);

    var resArray = new object[] { result ,result1};

    ServicePointManager.SecurityProtocol |= SecurityProtocolType.Ssl3 | 
    SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | 
    SecurityProtocolType.Tls;

    var client = new RestClient("http://127.0.0.1:8080");
    var request = new RestRequest("category", Method.POST);

    request.AddHeader("Content-Type", "application/json");
          
    request.AddJsonBody(new
    {
         categories = resArray,      
     }) ;

    var response = client.Execute(request);

    MessageBox.Show(response.IsSuccessful.ToString());
         
    MessageBox.Show(response.Content);

}

Upvotes: 1

LucasMetal
LucasMetal

Reputation: 1393

This should work:

request.RequestFormat = DataFormat.Json; // Important

var input = new Dictionary<string, object>();
// cats can be an array on real objects too
var cats = new[] {new {id = 9}, new {id = 14}};
input.Add("categories", cats);

request.AddBody(input);

Upvotes: 1

Anton Polchenko
Anton Polchenko

Reputation: 72

If I understand it right you want to post a JSON Array. If you don't want to form a JSON string manually the easiest way is to use Newtonsoft.Json

Here is an example to you:

List<int> data = new List<int>() // This is your array

string JsonData = JsonConvert.SerializeObject(data);

            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            StringContent content = new StringContent(JsonData, Encoding.UTF8, "application/json");

            HttpResponseMessage result = await client.PostAsync("your adress", content);

This is the easy way to make a POST request to a server.

To read the response you can use:

string answer = await result.Content.ReadAsStringAsync();

Upvotes: 0

Related Questions