简体   繁体   中英

Difference between RestSharp methods AddParameter and AddQueryParameter using HttpGET

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.

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.

To answer the OP and anyone else who might have trouble with the concept. It took me awhile to get around to the concept. You probably need to visualise the RESTful standard of how to construct a GET request message in a url against constructing for a POST request message.

You will notice that for GET , the parameter(s) are attached to the URL header whereas for the POST , the parameter(s) are placed in the body of the message. RestSharp 's method AddQueryParameter() will only add the (Query) parameters in the header of the message, whereas the AddParameter() will only add the parameters to the mesasge body. As demonstrated below the GET has one query parameter with a value of "Flavours" . For the POST , the parameters contact_name and company_name are located in the bottom of the message body.

Eg:

GET message format :

GET http://www.consumerdiarydemo.cbrnetwork.test.au/api/ConsumerDiary/getSizesOrFlavours/Flavours HTTP/1.1 Host: www.consumerdiarydemo.cbrnetwork.test.au Connection: keep-alive Accept: application/json User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Referer: http://www.consumerdiarydemo.cbrnetwork.test.au/ConsumerDiaryPage2template Accept-Encoding: gzip, deflate, sdch Accept-Language: en-GB,en-US;q=0.8,en;q=0.6


POST message format :

POST http://localhost:1234567/api/customers HTTP/1.1 Accept: application/json, text/javascript, / ; q=0.01 X-Requested-With: XMLHttpRequest Content-Type: application/x-www-form-urlencoded; charset=UTF-8

{"contact_name":"value_data1","company_name":"value_data2"}

AddParameter / Get or Post

GetOrPost behaves differently based on the method. If you execute a GET call, RestSharp will append the parameters to the Url in the form url?name1=value1&name2=value2 .

On a POST or PUT Requests, it depends on whether you have files attached to a Request. If not, the Parameters will be sent as the body of the request in the form name1=value1&name2=value2 .

Ref: https://restsharp.dev/usage.html#get-or-post

AddQueryParameter / Query String

QueryString works like GetOrPost , except that it always appends the parameters to the url in the form url?name1=value1&name2=value2 , regardless of the request method.

Ref: https://restsharp.dev/usage.html#query-string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM