Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to set my BaseUrl as a string but client.BaseUrl returns a URI, because I have my generic GET and POST methods that use it but I am running into this issue

private static bool setBaseURL(string baseURL)
      {
          if (baseURL == null || baseURL.Empty())
          {
              Console.WriteLine("Please pass the correct base URL....");
              return false;
          }
          try
          {
              RestClient client = new RestClient();
              client.BaseUrl = baseURI; //Here is where I want to set it
              return true;
          }
          catch (Exception e)
          {
              Console.WriteLine(e.Message);
              return false;
          }

      }



//Here is my GET and POST method for reference

public static IRestResponse doGet(string contentType, string baseURI, string basePath,
        string token, Dictionary<String, String> paramsMap)
    {
        if (setBaseURI(baseURI))
        {
            IRestRequest request = sendRequest(contentType, token, paramsMap);
            return getResponse("GET", request, basePath);
        }
        return null;

    }




    public static IRestResponse doPost(string contentType, string baseURI, string basePath,
       string token, Dictionary<String, String> paramsMap, Object obj)
    {
        if (setBaseURI(baseURI))
        {
            IRestRequest request = sendRequest(contentType, token, paramsMap);
            addRequestPayload(request, obj);
            return getResponse("POST", request, basePath);
        }
        return null;

    }


What I have tried:

I am not sure their is a method client.BaseHost .. not sure if I can use this
client.BaseHost = baseURI
Posted
Updated 13-May-20 5:54am

1 solution

Look at what your setBaseURL method is doing:
  1. Create a new RestClient instance;
  2. Set its BaseUrl property;
  3. Throw that instance away, and return true;

When the method returns, the changes you made to the local variable are not persisted anywhere. They won't have any effect on any subsequent instances of the RestClient class.

You need to set the BaseUrl on the same instance of IRestClient that you use to execute your requests. That means you'll need to modify your methods so that you pass the IRestClient instance in.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900