restsharp / RestSharp

Simple REST and HTTP API Client for .NET
https://restsharp.dev
Apache License 2.0
9.59k stars 2.34k forks source link

AddQueryParameter : Pipe character still encoded with false option #2207

Open ronando82 opened 4 months ago

ronando82 commented 4 months ago

Describe the bug i use AddQueryParameter with false attribute to prevent encoding my string but it doesn't work.

Example : .AddQueryParameter("ids", "in:001|116", false);

To Reproduce

var clientRest = new RestClient("http://xxx.yyy");
string resource = "/v1/xxxx/;
var request = new RestRequest(resource, Method.Get)
    .AddHeader("Accept", "application/json")
    .AddHeader("x-api-key", "myapikey")  
    **.AddQueryParameter("ids", "in:001|116", false);**

var responseRest =  clientRest.GetAsync<string>(request).Result;

the transmitted parameter is: ids=in:001**%7C**116

For example it's work with a slash / instead a pipe character |

    .AddQueryParameter("ids", "in:001/116", false);

the transmitted parameter is: ids=in:001/C116

Expected behavior

i would expect not encoding the pipe character

Desktop (please complete the following information):

alexeyzimarev commented 4 months ago

RestSharp doesn't encode query parameters when it is told not to. For example, this simple test passes:

    [Fact]
    public void Should_not_encode_pipe() {
        var client = new RestClient("http://example.com");
        var request = new RestRequest("resource");
        request.AddQueryParameter("ids", "in:001|116", false);

        var actual = client.BuildUri(request);
        var expected = new Uri("http://example.com/resource?ids=in:001|116");
        actual.Should().Be(expected);
    }

However, what I found now is that the Uri object encodes the pipe character. So, the actual URI values in both expected and actual vars in the test contain the same string with encoded pipe char, where the original URL string property for both of them contain non-encoded values.

I fixe in my PR but for .NET < 6 the code uses an obsolete Uri ctor. It works, just hoping they won't remove it.

alexeyzimarev commented 4 months ago

Sorry, closed prematurely. It still has issues with how some characters are encoded, it's different per framework. Will look at it again soon.