I encountered a very hidden issue which was about to force me to reject Flutter. It turned out that the http client sets followRedirect property to true by default. This causes some requests to auto load redirect pages. I need to parse all the cookies that a server sends me, but some of them were not processed just because of followRedirect set to true which leaded to skipping some redirecting requests since they were handled by the http client by its own
Could you please make it possible to pass the default value of followRedirects to the constructor or to the methods like .post(), .get() and so on?
You can customize this by setting BaseRequest.followRedirects (or by setting the http.io.follow_redirects context parameter on master).
@nex3 I can't disagree with you more. It's an http client! Not a web browser! It shouldn't be enabled by default
@Buckstabue And I disagree with what you're saying. It's an http client -- when I call client.get(), I expect to get the bytes of the resource back. I (usually) don't care if the server decides to do a series of redirects -- that's an implementation detail on the server side.
Now, in some cases, I do care. And that's why we have the choice to disable redirect following and handle it ourselves -- currently by creating our own Request object and using send(). But in the majority of cases I just want my bytes, and in that case, following redirects is the right thing to do -- which (I'm guessing) is why it's on by default.
(And like @nex3 said, once we get a new release of package:http, you'll be able to control followRedirects by passing in a context object with http.io.follow_redirects set to true or false when you call get(), post(), etc. Essentially what you asked for.)
@jakobr-google sorry, I misunderstood the answer. I thought that @nex3 suggested me to change source code in master branch. I hope the new version is going to be bundled in flutter project. Thank you!
I'm pretty sure Flutter will pick up the new version once it's released, but even if it doesn't, you can upgrade the dependency for your app by adding a dependency_overrides section to your pubspec.yaml:
dependency_overrides:
http: 0.12.0
(or whatever version you want). That will override Flutter's selected version, but like I said, I'm pretty sure Flutter will pick up the new version quickly.
You can start trying out the latest unreleased version right now by telling pub to get the package from Git instead:
dependency_overrides:
http:
git: git://github.com/dart-lang/http
I definitely wouldn't recommend shipping an app using an unreleased version of a package, but you can get a head start on adopting the latest version before it's released this way.
For now, though, I'd recommend using the version that ships with Flutter, and configuring your Request objects:
final request = new Request('GET', Uri.parse('https://example.com/'))
..followRedirects = false;
final response = await client.send(request);
What's missing in this discussion is the code snippet showing the use of context.
The advice to use followRedirects is not really helpful. You should note the use case: IOClient.get().
Not mentioning the difference in usability between Response and StreamedResponse.
@jakobr-google Thanks man.
Your answer really saved my day.
Most helpful comment
@nex3 I can't disagree with you more. It's an http client! Not a web browser! It shouldn't be enabled by default