How to get redirect url in Dart?

Viewed 3205

In my project I send simple request for authorization :

http.Response response = await http.get(url, headers: headers);

In response I has cookieID and other information, but I doesnt receive redirect url.

How I can take him?

2 Answers

As jamesdlin has mentioned above and this github issue mentions, http has redirects set to true by default. To avoid redirect followRedirects needs to be set to false. The redirectUri can be fetched as follows:

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

http.Request req = http.Request("Get", Uri.parse(YOUR_URL_HERE))..followRedirects = false;
http.Client baseClient = http.Client();
http.StreamedResponse response = await baseClient.send(req);
Uri redirectUri = Uri.parse(response.headers['location'])
Related