Search code examples
c#visual-studiorestsharp

How to pass a DateTime.Today as a parameter using RestSharp?


i'm making a console application to send email, im using the Qualtrics API REST, so i'm creating the request with RestSharp, the call that i'm using is for creating an email list, one of its parameters is the name of that list, but i want to put automatically the current date when it was created, this is the call:

 static void Main(string[] args)
  {
            var client = new RestClient("https://qualtrics.com/API/v3/mailinglists");
            var request = new RestRequest(Method.POST);
            request.AddHeader("Postman-Token", "XXXX");
            request.AddHeader("Cache-Control", "no-cache");
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("X-API-TOKEN", "XXXX");
            request.AddParameter("undefined", "{\r\n    \"category\": \"Test Category\",\r\n    \"libraryId\": \"XXXX\",\r\n    \"name\": \"Application Test\"\r\n}", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
  }

This part: "name\": \"Application Test\" is what i want to modify to put in "Application Test" the current date, Is there a way to do this?


Solution

  • \"date\":" + DateTime.Today.ToString()
    

    will give you want you want.

    P.S. It would be better in future not to build your JSON by hand but to serialise it from a C# object.