flutter: HTTP get request - disable encoding parameters
I believe you should replace:
var uri = Uri.parse('https://demo.com/rest/v1/default/products');
uri = uri.replace(queryParameters: params);
print(uri);
with:
var uri = Uri.https('demo.com', '/rest/v1/default/products', params);
more on this: Uri.https
more on: replace
example result:
regardless of this, if I try with your params, the library behaves normal and encodes the special characters. (see more here)
if we put the actual request in the browser to check the response:
https://demo.mage-mobile.com/rest/v1/default/products?searchCriteria[filter_groups][0][filters][0][condition_type]=in&searchCriteria[filter_groups][0][filters][0][field]=type_id&searchCriteria[pageSize]=20&searchCriteria[filter_groups][0][filters][0][value]=simple%2Cconfigurable%2Cbundle&searchCriteria[currentPage]=1&searchCriteria[sortOrders][0][field]=created_at&searchCriteria[sortOrders][0][direction]=DESC
we get the following response:
And this brings me to my initial suspicion: the API does not support this call.
Maybe you should also check this type of param from your code: 'searchCriteria[filter_groups][0][filters][0][condition_type]', it seems you are trying to acces some information from a collection but you actually writing a string... try removing the quotes (' bla bla ') from these params id... also try to put the request direcly in the browser(or postman) to see it work.
About the encoding (changing [ to %5B) -- this is normal and it should happen.
Trung Hoang
I'm a software developer and I always want to learn more about programming. It's my professional.
Updated on December 14, 2022Comments
-
Trung Hoang over 1 year
I'm trying to make a demo app with flutter and trying to fetch products from a demo magento site. This is my code:
Future<List<Product>> fetchProducts() async { final params = <String, String>{ 'searchCriteria[filter_groups][0][filters][0][condition_type]': 'in', 'searchCriteria[filter_groups][0][filters][0][field]': 'type_id', 'searchCriteria[pageSize]': '20', 'searchCriteria[filter_groups][0][filters][0][value]': 'simple,configurable,bundle', 'searchCriteria[currentPage]': '1', 'searchCriteria[sortOrders][0][field]': 'created_at', 'searchCriteria[sortOrders][0][direction]': 'DESC' }; var uri = Uri.parse('https://demo.com/rest/v1/default/products'); uri = uri.replace(queryParameters: params); print(uri); final response = await http.get(uri, headers: {HttpHeaders.authorizationHeader: "Bearer qb7157owxy8a29ewgogroa6puwoafxxx"}); if (response.statusCode == 200) { // If the call to the server was successful, parse the JSON. final data = json.decode(response.body); final products = data["items"] as List; return products.map<Product>((json) => Product.fromJson(json)).toList(); } else { // If that call was not successful, throw an error. throw Exception('Failed to load post'); } }
When I debugged, the response was 400 - Bad request. I guess that because the uri was encoded to include percentage characters as I printed as below:
So how can I disable encoding the uri? Thank you, guys.
-
Trung Hoang over 4 yearsHi, thank you. I tried with this but it seems to face some problems when I put my URL and params into. I debugged and see it go through this line and jump to the bottom of the function and passed the remaining codes. It's strange. Could you help to try this code:
var uri = Uri.https('https://demo.mage-mobile.com', '/rest/v1/default/products', params); print(uri);
It's stoped in the uri assignment line of code. Thanks. -
Trung Hoang over 4 yearsThank you, please try the params on my post above. I think the difference is that having [] characters.
-
Trung Hoang over 4 yearsHi, you are right, Durdu. How careless of me, the API had the wrong path. I tried again with my original code with
uri.replace()
function and it's worked. But it's strange that the functionuri.https()
is not working. If you have time, please try to put aprint(uri)
after this call and see nothing print out. -
Durdu over 4 yearsit prints out the url that i've put in the browser (but encoded).