flutterdarthttphttp-redirectdart-http

How to not follow redirects with Darts http-package? request.followRedirects = false is set


I'm trying to implement a small http wrapper with Flutter and Dart as follows:

import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;

class HttpWrapper {
  http.Client httpClient = http.Client();

  Future<http.StreamedResponse> backendHttpRequest(
      String method, String endpoint) async {
    String backendUrlBase = const String.fromEnvironment("backend_url_base");

    var uri = Uri.parse(backendUrlBase + endpoint);
    http.Request request = http.Request(method, uri);
    request.followRedirects = false;

    var response = await httpClient.send(request);

    if (response.isRedirect) {
      debugPrint("Redirecting...");
      // handle redirect
      return response;
    } else {
      return response;
    }
  }
}

I want to handle redirects separately, since the server may redirect me to an endpoint which does not accept XMLHttpRequests, which runs me into CORS issues then. This wouldn't be a problem if I could just handle the redirect response on my own, as I tried with handling response.statusCode == 302 - my problem is that request.followRedirect = false does not seem to have an effect at all. The client just follows the redirects, no matter what I set followRedirects.

How can I solve this, that my request does not follow redirects?

Thank you for your help!

Edit 1: Changed response.statusCode == 302 to response.isRedirect

Edit 2: How I produce the problem:

I have set up an example server in express as follows:

const express = require('express');
const app = express();
var cors = require('cors')
const logger = require('morgan');
const PORT = 3000;

app.use(logger('dev'));
app.use(cors()) 

// Without middleware
app.get('/endpointwhichredirects', function (req, res) {
    console.log("Redirecting...");
    res.redirect('/user');
});
 
app.get('/user', function (req, res) {
    res.send("This is the user-page");
});
 
app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});

I use my http-wrapper in Dart as follows:

HttpWrapper httpWp = HttpWrapper();
res = httpWp.backendHttpRequest('Get', '/endpointwhichredirects');

My server logs the following when this Dart-code gets executed:

> GET /endpointwhichredirects 302 3.051 ms - 27
> GET /user 304 2.986 ms - -

The debug print "Redirecting..." never gets calles. If I set a breakpoint for both returns of response only the one in the else branch gets caught, never the one handling the redirect.


Solution

  • I just found the explanation in the docs:

    A dart:html-based HTTP client that runs in the browser and is backed by XMLHttpRequests. This client inherits some of the limitations of XMLHttpRequest. It ignores the BaseRequest.contentLength, BaseRequest.persistentConnection, BaseRequest.followRedirects, and BaseRequest.maxRedirects fields. It is also unable to stream requests or responses; a request will only be sent and a response will only be returned once all the data is available.

    Reason seems to be that with XMLHttpRequest redirects are transparent followed by the browser.