While using RestSharp (107.3.0) I've encountered a hurdle with it's URL encoding. My code is spread across several classes but looks something like this...
var student = new Student()
{
Code = "123456",
Name = "CABEÇA, ANNA",
CourseCode = "CRS00199",
Enrolled = "2022-07-27 12:25:57"
};
var clientOptions = new RestClientOptions()
{
BaseUrl = new Uri("https://myapi")
};
var client = new RestClient(clientOptions);
var request = new RestRequest("student/{c}", Method.Put);
request.AddUrlSegment("c", student.Code);
request.AddQueryParameter("n", student.Name);
request.AddQueryParameter("cc", student.CourseCode);
request.AddQueryParameter("e", student.Enrolled);
var url = client.BuildUri(request);
This is working fine for 99.999% of the students, but for some with non-ASCII characters in their names, like the above (fake) student, the query parameters are not fully encoded within the resulting URL.
https://myapi/student/123456?n=CABEÇA%2c ANNA&cc=CRS00199&e=2022-07-27 12%3a25%3a57
I would expect the Ç in the student's name to be encoded as %c7.
Is there a "better" way to be doing this? Have I missed something obvious? I also see that spaces aren't being encoded, but commas, periods, and colons are.
Many thanks.
--- Update following Magnetron's answer --- In a calling class, I was actually doing a ToString() on the returned Uri.
url.AbsoluteUriinstead ofurl.ToString()ToStringis a mix of encoded and unencoded. It would make more sense if it was all encoded or all unencoded, maybe it's some sort of backwards compatibility, don't really know.