Please help me with my question
http://stackoverflow.com/questions/40632827/how-to-disable-urlencoding-get-params-in-refit
@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,
If you can麓t go through the work of duplicating you question, then it麓s most like nobody sees the issue as important. If you really needed help here you麓d take the time to post here and also include a link to SO
Instead of ignoring you, i opted on giving feedback.
First of all was your api server able to parse the follow?
api/item?c%5B%5D=14%26c%5B%5D%3D74
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}")]
Task
The url argument itself also contains a querystring. (for example GetDocument(url: "download.php?filename=somefile.pdf");
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
HTTP GET /download?filename=somefile.pdf
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.
Most helpful comment
This is a must-have one. :)