How do you correctly add query parameters to a Dart http get request? I been unable to get my request to respond correctly when trying to append the '?param1=one¶m2=two' to my url, yet it works correctly in Postman. Here's the gist of my code:
final String url = "https://www.myurl.com/api/v1/test/";
String workingStringInPostman = "https://www.myurl.com/api/v1/test/123/?param1=one¶m2=two";
Map<String, String> qParams = {
'param1': 'one',
'param2': 'two',
};
var res = await http
.get(Uri.encodeFull("$url${widget.pk}/"),
headers: {HttpHeaders.authorizationHeader: "Token $token",
HttpHeaders.contentTypeHeader: "application/json"},
);
The ${widget.pk} is simply a integer value being pass (See the value 123 in the workingStringInPostman variable.
The qParams is there for connivence, in case a Uri parameter is needed.
A code example would be welcomed.
The accepted answer didn't work for me but adding a '&' without quotes to end of the URL solves my problem. In this case, change the following line:
to this: (Notice the '&' at the end).
Got the same question. The accepted answer won't work if my url is localhost with port like
https://localhost:5001
. After spending 1 day to search for solution, I come up with Dio library. Following is my solution usingDio
:Hope this helps.
You'll want to construct a
Uri
and use that for the request. Something likeSee https://api.dartlang.org/stable/2.0.0/dart-core/Uri/Uri.https.html
There is a dart package that provides some helper classes for http requests.
BasicUtils : https://github.com/Ephenodrom/Dart-Basic-Utils
Install it with:
Usage
You can add a map of headers and query parameters to each request. See the example :
Additional information :
These are all methods from the HttpUtils class.