سؤال

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 :(

هل كانت مفيدة؟

المحلول 3

In the RestSharp sources I found out that the parameters are always encoded (both the name and the value), so I guess that I will have to fork it if I want to add an additional parameter.

نصائح أخرى

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);

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);

See this PR from the project site: https://github.com/restsharp/RestSharp/pull/1157

However, as far as I can tell, it's not yet in a release on NuGet.

Update: probably doesn't work in most cases from comments.

I found an interesting solution... Just decode the parameters you want to pass in and restsharp will encode back to what it should be. For example, I have an api key that uses %7 in it and RestSharper further encodes it. I decoded the api key and passed that into RestSharp and it seems to work!

This solution worked for me

request.AddQueryParameter("sendEndDate", "string:data,something-else", false);

This is the function in the metadata of RestSharp.IRestRequest:

IRestRequest AddQueryParameter(string name, string value, bool encode);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top