Govaadiyo
Govaadiyo

Reputation: 6082

Flutter http.get with path parameter issue

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?

Upvotes: 5

Views: 12988

Answers (3)

benten
benten

Reputation: 790

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 protected]&receiver_id=2`


 String sendNotification = "your data";

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

       await http.get(uri);

Upvotes: 0

Lesiak
Lesiak

Reputation: 25936

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);

Upvotes: 6

Philip Hahn
Philip Hahn

Reputation: 138

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

Upvotes: 2

Related Questions