Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not encode parameters in RestSharp

Tags:

restsharp

I am using RestSharp to access a RubyOnRails API. As you might know, RoR likes when the parameters names are in the form model_name[property]. RestSharp, on the other hand, does not like it.

Fiddler says I send this to the server :

user%5Bemail%5D=user%40email.com&user%5Bpassword%5D=test

It looks like R# encodes both the parameters and values when it sends the data (unlike Curl, which looks like it encodes selectively). While that's fine most of the time I guess, in this particular case, it makes the API return a 401 because it doesn't understand the parameters.

Is it possible to ask R# to not encode the request's parameters ?

Thank you !

Edit

Ok, in the R# sources, I found the method RestClient.EncodeParameters, so it looks like the parameter's name is always encoded. I guess I will have to fork it :(

like image 949
thomasb Avatar asked Feb 28 '14 15:02

thomasb


2 Answers

Since RestSharp version 106.4.0 you can just use ParameterType.QueryStringWithoutEncode in request.AddParameter() function:

        request.AddParameter("user_id", @"45454545%6565%65", ParameterType.QueryStringWithoutEncode);
like image 79
Ruben Martirosyan Avatar answered Oct 17 '22 22:10

Ruben Martirosyan


I know this has already been answered, but I wanted to add this answer since it worked for me. There is an (albeit hacky) solution to this. Build your own uri for parameters that should not be encoded.

var uri = string.Concat("/path/to/your/api", "?paramThatShouldNotBeEncoded=", DateTime.Now.Date.AddDays(1).ToString("O"));
var restRequest = new RestRequest(uri, Method.GET);
like image 5
birdamongmen Avatar answered Oct 18 '22 00:10

birdamongmen