45
votes

I am trying to make an HTTP POST request with the flutter plugin HTTP but I am getting an error of the title. Does anyone know the cause of this since in my other applications this works just perfectly fine?

await http.post(Uri.encodeFull("https://api.instagram.com/oauth/access_token"), body: {
      "client_id": clientID,
      "redirect_uri": redirectUri,
      "client_secret": appSecret,
      "code": authorizationCode,
      "grant_type": "authorization_code"
    });
2

2 Answers

118
votes

To improve compile-time type safety, package:http 0.13.0 introduced breaking changes that made all functions that previously accepted Uris or Strings now accept only Uris instead. You will need to explicitly use Uri.parse to create Uris from Strings. (package:http formerly called that internally for you.)

Old Code Replace With
http.get(someString) http.get(Uri.parse(someString))
http.post(someString) http.post(Uri.parse(someString))

(and so on.)

In your specific example, you will need to use:

await http.post(
  Uri.parse("https://api.instagram.com/oauth/access_token"),
  body: {
    "client_id": clientID,
    "redirect_uri": redirectUri,
    "client_secret": appSecret,
    "code": authorizationCode,
    "grant_type": "authorization_code",
  });
2
votes

If you don't want to use parse and want to go with simple way, here is the example:

Suppose you want to get some data from :- https://example.com/helloworld

It works for me:

String url ='example.com';
int helloworld=5;
http.client;
client.get(Uri.https(url,'/$helloworld'),
  headers:{Content-type':'application/json',
  },
 );

If you want to get: http://example.com/helloworld

instead of

https://example.com/helloworld

you just have to change Uri.http(), and that's all.

It is correct that. http.get or http.post now accepting only Uris , but that is not a big deal as you see.