Do not encode parameters in RestSharp

自闭症网瘾萝莉.ら 提交于 2019-12-01 19:59:50

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

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.

Ruben Martirosyan

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

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!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!