-
Notifications
You must be signed in to change notification settings - Fork 0
ParameterTypes for RestRequest
After you’ve created a RestRequest, you can add Parameters to it.
Here is a Description of the 5 currently supported types and their behavior when using the default IHttp implementation.
Adds the parameter to the list of cookies that are sent along with the Request. The Cookie Name is the name of the Parameter and the Value is the Value (.ToString) you passed in.
Adds the parameter as a HTTP header that is sent along with the request. The Header Name is the name of the Parameter and the Header value is the Value.
Note that there are some restricted headers that may behave differently or that are simply ignored. Please look at the _restrictedHeaderActions dictionary in Http.cs to see which headers are special and how they behave.
This 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 or not 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”. Also, the request will be sent as application/x-www-form-urlencoded.
In both cases, Name and Value will automatically be UrlEncoded.
If you have files, RestSharp will send a multipart/form-data request. Your Parameters will be part of this request in the form
Content-Disposition: form-data; name=“parameterName”
ParameterValue
Unlike GetOrPost, this ParameterType replaces placeholder values in the RequestUrl:
var rq = new RestRequest("health/{entity}/status"); rq.AddParameter("entity", "s2", ParameterType.UrlSegment);
When the request executes, RestSharp will try to match any {placeholder} with a parameter of that name (without the {}) and replace it with the value. So the above code results in “health/s2/status” being the Url.
If this parameter is set, it’s value will be sent as the body of the request. The name of the Parameter is ignored, and so are additional RequestBody Parameters – only 1 is accepted.
RequestBody only works on POST or PUT Requests, as only they actually send a body.
If you have GetOrPost parameters as well, they will overwrite the RequestBody – RestSharp will not combine them but it will instead throw the RequestBody parameter away.