Using different parameters for POST request in second method

prayank 21 Reputation points
2021-12-18T08:52:45.707+00:00

In the code below, GetResults() is using value of response.Content from GetAddress():

   "{\"result\":\"someaddress\",\"error\":null,\"id\":\"rpctest\"}\n"  

This is wrong as it should be using new request parameters defined in the method. So I get this error when I run the application:

   System.Text.Json.JsonException: 'The JSON value could not be converted to System.Collections.Generic.List`1[Myapp.mainResult]. Path: $.result | LineNumber: 0 | BytePositionInLine: 54.'  

I tried adding this in GetResults() before adding new parameters but it does not work and gives null JSON error:

   restParams.request.Parameters.Clear();  

 

   c#  
     
       namespace Myapp  
       {  
           public class mainResult  
           {  
               public string address { get; set; }  
               public string email { get; set; }  
               public bool remote { get; set; }  
           }  
         
           public class Params  
           {  
               public RestClient? client { get; set; }  
               public RestRequest? request { get; set; }  
           }  
         
           public class Addresses  
           {  
               public string? address { get; set; }  
           }  
         
           public class addResponse  
           {  
               public string result { get; set; }  
               public object error { get; set; }  
               public string id { get; set; }  
           }  
         
           public class mainResponse  
           {  
               public List<mainResult> result { get; set; }  
               public object error { get; set; }  
               public string id { get; set; }  
           }  
         
           public partial class MainWindow : Window  
           {  
               private static Params restParams = new Params();  
               private static Addresses home_addresses = new Addresses();  
               private static addResponse addResponse = new addResponse();  
               private static mainResponse mainResponse = new mainResponse();  
         
               public MainWindow()  
               {  
                   InitializeComponent();              
         
                   if (ConnectServer())  
                   {  
                       new_address.Content = GetAddress();  
                       SaveAddress();  
                   }  
               }  
         
               public static bool ConnectServer()  
               {  
                   try  
                   {  
                       restParams.client = new RestClient("http://localhost:8080");  
                       restParams.client.Timeout = -1;  
                       restParams.request = new RestRequest(Method.POST);  
                       restParams.request.AddHeader("Authorization", "Basic ZDhjZGUyNDIzNDhhNmQwNTIwZGI1ZjcyNjk");  
                       restParams.request.AddHeader("Content-Type", "application/json");  
                   }  
                   catch (Exception ex)  
                   {  
                       Console.Write(ex.ToString());  
                       return false;  
                   }  
                   return true;  
               }  
         
               public static string GetAddress()  
               {  
                   const string? body = @"{""jsonrpc"": ""1.0"", ""id"": ""rpctest"", ""method"": ""getaddress"", ""params"": [""test""]}";  
                   restParams.request.AddParameter("application/json", body, ParameterType.RequestBody);  
                   IRestResponse response = restParams.client.Execute(restParams.request);  
         
                   addResponse newaddress = JsonSerializer.Deserialize<addResponse>(response.Content);  
         
                   home_addresses.address = newaddress.result;              
         
                   return home_addresses.address;  
               }  
         
               public static async void SaveAddress()  
               {  
                   string address = home_addresses.address;  
         
                   string json_path = @"E:\address_test.json";  
         
                   if (!File.Exists(json_path))  
                   {  
                       await using (StreamWriter writer = File.CreateText(json_path))  
                       {  
                           writer.WriteLine(address);  
                       }  
                   }  
                   else  
                   {  
                       await using (StreamWriter writer = File.AppendText(json_path))  
                       {  
                           writer.WriteLine(address);  
                       }  
                   }  
               }  
         
               public static List<mainResult> GetResults()  
               {  
                     
                   const string? body = @"{""jsonrpc"": ""1.0"", ""id"": ""rpctest"", ""method"": ""getresults"", ""params"": []}";  
                   restParams.request.AddParameter("application/json", body, ParameterType.RequestBody);  
         
                   IRestResponse response = restParams.client.Execute(restParams.request);  
         
                   mainResponse main_response = JsonSerializer.Deserialize<mainResponse>(response.Content);  
         
                   mainResponse.result = main_response.result;  
         
                   return mainResponse.result;  
               }  
         
               private void getresults_button_Click(object sender, RoutedEventArgs e)  
               {  
                   mylist.AppendText(GetResults().ToString());  
               }  
           }  
       }  

What is wrong with the code and how do I fix this?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,008 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ken Tucker 5,856 Reputation points
    2021-12-18T19:23:36.403+00:00

    you are reusing the restParams in your class. The second time you call make a rest you are adding a second application/json parameter causing the error. You need to do one of 2 things create a method to create the restParams

    This is what I think needs to be reused

               public Params GetRestParams()
               {
                    var restParams = new Params();
                    restParams.client = new RestClient("http://localhost:8080");
                    restParams.client.Timeout = -1;
                    restParams.request = new RestRequest(Method.POST);
                    restParams.request.AddHeader("Authorization", "Basic ZDhjZGUyNDIzNDhhNmQwNTIwZGI1ZjcyNjk");
                    restParams.request.AddHeader("Content-Type", "application/json");
                    return restParams;
              }
    

    and in when you make a call try something like this instead

               const string? body = @"{""jsonrpc"": ""1.0"", ""id"": ""rpctest"", ""method"": ""getaddress"", ""params"": [""test""]}";
                var restParms = GetRestParams();
                restParams.request.AddParameter("application/json", body, ParameterType.RequestBody);
    

    Other option is to loop through the Parameters in restParam and update the value of the application/json one if it is there

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.