Do not encode parameters in RestSharp
Asked Answered
B

6

6

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

Blunderbuss answered 28/2, 2014 at 15:13 Comment(0)
B
5

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.

Blunderbuss answered 4/3, 2014 at 8:35 Comment(1)
Updated line number: github.com/restsharp/RestSharp/blob/master/RestSharp/…Odious
P
14

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);
Peppy answered 25/2, 2019 at 15:13 Comment(2)
This should now be the accepted solution - since 106.4.0 came out.Thenar
Yeah, I think soPeppy
B
5

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.

Blunderbuss answered 4/3, 2014 at 8:35 Comment(1)
Updated line number: github.com/restsharp/RestSharp/blob/master/RestSharp/…Odious
L
5

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);
Lat answered 20/5, 2016 at 20:25 Comment(0)
S
0

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!

Selfsealing answered 22/4, 2014 at 22:13 Comment(1)
That is unfortunate, this might have been an edge case. I believe it was an error with a MapQuest API key that I resolved by doing this.Selfsealing
S
0

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.

Secateurs answered 10/10, 2018 at 15:5 Comment(0)
P
0

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);
Prevention answered 26/3, 2021 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.