Search code examples
c#restsharp

Convert string to a RestSharp defined call method


So I'm trying to use RestSharp and create a function that handles everything and then call it alone, instead of using every single function in restsharp everytime. What I'm having trouble with is having a String parameter be used as a definition inside Method of RestRequest.

    public void GetRestClient(string country, string method, string endpoint, string body = null) 
    {
        string url = getCountryUrl(country) + endpoint;
        RestClient client = new RestClient(url);
        RestRequest getRequest = new RestRequest(Method.**method**);
    }

Would this be a good way?

        RestRequest getRequest = new RestRequest((Method)Enum.Parse(typeof(Method), method));

Solution

  • This worked:

     RestRequest getRequest = new RestRequest((Method)Enum.Parse(typeof(Method), method));