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?

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

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 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?
I removed as per your suggestion and print like "xx.yy/api/user?slug=govadio" but not working :( curious working on Postman but not in dart.. Thanks for helping any other ways?
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 protected]&receiver_id=2`


 String sendNotification = "your data";

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

       await http.get(uri);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.