5

If I try to run on Postman, It works perfectly. Look at below image.

enter image description here

You can see, below is url

https://xx.yy/api/user/:slug

Path parameter is

slug

My code in Flutter, Doesn't work!

    final _authority = "xx.yy";
    final _path = "api/user/:slug"; // Tried to replace "api/user/slug" AND "api/user"
    final _params = { "slug" : "govadiyo" };
    final _uri =  Uri.https(_authority, _path, _params);

    print(Uri.encodeFull(_uri.toString()));
    var response = await http.get(Uri.encodeFull(_uri.toString()), headers: {'Content-Type': 'application/json'});
    print(response.body);

Anything is going wrong with above code?

Govaadiyo
  • 5,644
  • 9
  • 43
  • 72

3 Answers3

6

As you correctly noticed, you need a path variable, not a query param (that means your variable becomes part of the url).

You can use string interpolation to put your variable into the url (in fact, concatenation would work as well). The variable may contain characters that need to be encoded.

final slug = 'govadiyo';
final url = Uri.encodeFull('api/user/${slug}');
print(url);
Lesiak
  • 22,088
  • 2
  • 41
  • 65
2

have a look at this answer. Seems the question is pretty the same as yours: https://stackoverflow.com/a/52824562/11620670

Get rid of your param in the _path variable.

The _uri variable seems to be well structured.

After this small change it should work. So does the example in the linked answer.

Greetings

Philip Hahn
  • 138
  • 1
  • 8
  • Thanks for the answer but I'm sorry, I tried with this one but same result and In my question I already split up the host and path. – Govaadiyo Jun 11 '19 at 06:08
  • @Govaadiyo That you split up the host and the path it totally fine. What I mean was, that you change your _path to `api/user` without the `/:slug`. You defined your params well in _params. I think it's just an issue with path. What does your path say in your first print statement? – Philip Hahn Jun 11 '19 at 06:27
  • I removed as per your suggestion and print like "https://xx.yy/api/user?slug=govadio" but not working :( curious working on Postman but not in dart.. Thanks for helping any other ways? – Govaadiyo Jun 11 '19 at 06:58
0

best way to send data in http.get() like this in 2021 you can send any type of data like Map, String any type you want just put data in sendNotification argument

http://www.google.com/hitnotification?sender_name=fahad&email=fahad@gmail.com&receiver_id=2`


 String sendNotification = "your data";

final uri = Uri.http('www.google.com','/hitnotification'+sendNotification);

       await http.get(uri);
benten
  • 696
  • 7
  • 18