Home > Back-end >  Calling RestAPI RestSharp GET method with parameters in C#
Calling RestAPI RestSharp GET method with parameters in C#

Time:04-03

It is probably really easy to solve, but I am struggling with it a lot.

I am connected to API using RestSharp - everything is working fine, but I do not know how to pass parameters to a method 'couse they are in a weird format (at least for me, I am newbie :)).

Te method's parameter should look like that:

"order_id": 123456

http://api.baselinker.com - API documentation

My testing code:

using RestSharp;
using Newtonsoft.Json;

var client = new RestClient("https://api.baselinker.com/connector.php");
string token = "********************";


RestRequest request = new RestRequest();

var parameter = ;

request.AddParameter("token", token);
request.AddParameter("method", "getOrders");
request.AddParameter("parameters", "order_id": 123456);



var response = await client.PostAsync(request);

Console.WriteLine(response.Content);

Of course this line is getting error 'cause VS is expecting ','

request.AddParameter("parameters", "order_id": 123456);

EDIT: Passing parameter as a string also doesn't work:

request.AddParameter("parameters", @"""order_id"": 123456");

What is the proper way to do it? Thanks for help ;)

CodePudding user response:

is it what you want?

request.AddJsonBody(
    new 
    {
      "order_id": 123456
    }); // AddJsonBody serializes the object automatically

or another way to write:

request.AddParameter(
   "application/json",
   "{ \"order_id\": 123456 }", // <- your JSON string
   ParameterType.RequestBody);
  • Related