Darran Mullen
Darran Mullen

Reputation: 79

Get URL of a http get request in flutter app

I am trying to get the URL of the following get request;

String url = ['url_here'];
Future<Post> fetchPost() async {
final response = await http.get(url);

if (response.statusCode == 200) {
return Post.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to load post');
}
}

I get a statuscode of 200.

Any ideas?

To clarify, When you enter the url in a browser it brings me to a page with a url that has a code in it. I am trying to extract that code.

Upvotes: 1

Views: 5794

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657118

followRedirects is true by default and I haven't found a way to get the URL it redirects to this way.

Setting followRedirects = false returns the new address in the location header.

  final url = 'http://wikipedia.net';
  final client = http.Client();
  final request = new http.Request('GET', Uri.parse(url))
    ..followRedirects = false;
  final response = await client.send(request);

  print(response.headers['location']);
  print(response.statusCode);

If you want to fetch the content of the redirect address, you can send a new request with the URL from location or just with the default (followRedirects = true)

Upvotes: 5

Related Questions