-
-
Notifications
You must be signed in to change notification settings - Fork 746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to disable urlencoding get-params in Refit? #277
Comments
@onovotny please help |
@onovotny is quite a busy developer in terms of opensource project he actively contributes to (like everyone else), So navigating to GitHub and then navigating SO isn´t work that someone paid for, Instead of ignoring you, i opted on giving feedback. Regarding the issue(link), if you don´t mind me taking a shot at it instead of @onovotny 😃 then i suggest:First of all was your api server able to parse the follow? Encoding is great for avoiding code injection to your server. This is something Refit is a bit opinionated about, i.e uris should be encoded, the server should be upgraded to read encoded uris. But this clearly should be a opt-in settings in Refit but it´s not. So you can currently do this by using a DelegatingHandler: /// <summary>
/// Makes sure the query string of an <see cref="System.Uri"/>
/// </summary>
public class UriQueryUnescapingHandler : DelegatingHandler
{
public UriQueryUnescapingHandler()
: base(new HttpClientHandler()) { }
public UriQueryUnescapingHandler(HttpMessageHandler innerHandler)
: base(innerHandler)
{ }
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var uri = request.RequestUri;
//You could also simply unescape the whole uri.OriginalString
//but i don´t recommend that, i.e only fix what´s broken
var unescapedQuery = Uri.UnescapeDataString(uri.Query);
var userInfo = string.IsNullOrWhiteSpace(uri.UserInfo) ? "" : $"{uri.UserInfo}@";
var scheme = string.IsNullOrWhiteSpace(uri.Scheme) ? "" : $"{uri.Scheme}://";
request.RequestUri = new Uri($"{scheme}{userInfo}{uri.Authority}{uri.AbsolutePath}{unescapedQuery}{uri.Fragment}");
return base.SendAsync(request, cancellationToken);
}
} Then you could use it as follows: Refit.RestService.For<IYourService>(new HttpClient(new UriQueryUnescapingHandler())) I recommend posting the question here as well, the suggested answer just don´t make sense without it. Cheers |
There may be valid reasons for disabling the automatic UrlEncoding, but it's not something that's currently available. If someone wants to try a PR to make it optional by parameter (an additional attribute?), I'd certainly entertain it. |
This is a must-have one. :) |
@Deilan go ahead and make a PR for it. It is marked up for grabs ;) |
I'm trying to use Refit to download a file from a dynamic (but relative to the base) url. [Get("/{**url}")] I notice my url argument gets encoded so I get an 404 error. Will this be possible after @deesejohn 's pullrequest gets merged? |
@pieteckhart you should write the following instead: [Get("/download")]
Task<Stream> GetDocument(string filename); calling the following [Get("/download")]
var filestream = await client.GetDocument(filename: "somefile.pdf"); would result in
hope it makes sense to you. If it doesn´t, please comment on, or let wait for @deesejohn 😊 |
Thanks for the suggestion, but the thing is that the download url is provided to me as is by another API call. I have no influence on the API so I was looking for a way to just blindly use the url without taking it apart and construct a proper request. For now I've hand rolled a request using the same httpClient so that it has access to a session cookie. |
I actually closed that PR because I wasn't very happy with the implementation. I've since migrated my project to RestEase as it was already able to satisfy my requirements. @benjaminhowarth1 I'm actually unable to reply to you on the PR directly, now that is has been locked, but it was never merged. If you and/or others feel the implementation is satisfactory, please feel free to merge it. |
Unescape Querystring parameters #277
Please help me with my question
http://stackoverflow.com/questions/40632827/how-to-disable-urlencoding-get-params-in-refit
The text was updated successfully, but these errors were encountered: