Difference between RestSharp methods AddParameter and AddQueryParameter using HttpGET

42,832

To answer your question

AddQueryParameter adds a parameter in the query string as ParameterType.QueryString whereas AddParameter(string, object) adds the parameter as ParameterType.GetOrPost

For more details on each parameter type, see:

GetOrPost: https://github.com/restsharp/RestSharp/wiki/ParameterTypes-for-RestRequest#getorpost

QueryString: https://github.com/restsharp/RestSharp/wiki/ParameterTypes-for-RestRequest#querystring

To solve your problem

It seems it is unrelated to the type of parameter, because the exception thrown seems to indicate you aren't even connecting to the remote server.

make sure you pass the same apiUrl / myUrl in both cases.

Share:
42,832
Nic
Author by

Nic

Updated on July 09, 2022

Comments

  • Nic
    Nic almost 2 years

    I'm using RestSharp to call an external API.

    This works:

    var client = new RestClient(apiUrl);
    var request = new RestRequest(myurl, Method.GET);
    
    foreach (var param in parameters)
    {
        request.AddQueryParameter(param.Key, param.Value);
    }
    var response = client.Execute(request);
    

    This doesn't:

    var client = new RestClient(apiUrl);
    var request = new RestRequest(myurl, Method.GET);
    
    foreach (var param in parameters)
    {
        request.AddParameter(param.Key, param.Value);
    }
    var response = client.Execute(request);
    

    Resulting in:

    System.Exception: API Call MyWebAPIMethod GET: Failed with status code 0 - Unable to connect to the remote server

    What's the difference between AddParameter and AddQueryParameter?

    According to the documentation they should function the same when using HttpGET and according to Fiddler they seem to generate the same URL as well.