1

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.

4
  • can you add some of your logs Commented Feb 22, 2019 at 17:06
  • what would you like to see? Commented Feb 22, 2019 at 17:14
  • Are you using HTTP from dart:io or from package:http? Commented Feb 22, 2019 at 17:28
  • import 'package:http/http.dart' as http; Commented Feb 22, 2019 at 17:29

1 Answer 1

5

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)

Sign up to request clarification or add additional context in comments.

6 Comments

is redirect is false
What is the response code? Is the location header set?
Is it a public URL that I can test myself with?
response.statusCode = 200, and no the location header is not set
its not public. i'm trying to connect fitbit to my app. on their OAuth 2.0 tutorial page they build the url with clientsecret etc. they say: Copy and paste the code that you can find in the redirect URL after the user has clicked the "allow" button. Example: localhost:8888/…, the code you need to paste from that example is 7b64c4b088b9c841d15bcac15d4aa7433d35af3e. Don't include the “#_=_”.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.