0

Context

I have an API (http) that i can access with the GET method via Postman without any issue. I wanted to fetch data from this API for my flutter app using the http package.

Issue

While making the network request, with http.get() method of the http package, like below, I couldn't fetch data from the API.

Future<Product> fetchProducts(String barcodeId) async {
var vPath = '/products/' + barcodeId;
var vUri = new Uri(
  scheme: 'http',
  host: 'product.acirfa.tech',
  port: 3000,
  path: vPath,
);
// /products/224213463241
print(vPath);
// http://product.acirfa.tech:3000/products/224213463241
print(vUri);
var response = await http.get(vUri);
print(response);
if (response.statusCode == 200) {
  print('OK');
} else {
  print('KO');
}}

I have printed out the URI (vUri) that I passed as argument to the http.get() method and by clicking on it, I get the data via browser. The print out of the variable "response" is not executed, which makes me think the issue comes from vUri. Although, I have tried to create the URI with the URI Http constructor[Uri.http()], I saw the same behaviour.

Expected Result

I expected to see the print out OK

Any thoughts on where i am going wrong ? Thanks :)

mbr512
  • 11
  • 3

1 Answers1

0
final _uri = Uri.http('www.domainName.com', 'furtherLink/page.php', {'key': '$value'});

final response = await http.get(
        _uri, 
        headers: {'authorization':'$_auth'}
).catchError((error) {
  throw error;
});

if (response.statusCode == 200) {
  print('OK');
} else {
  print('KO');
}}

Since you are using http & not https, it is considered unsecured. Make sure to pass the permission.

NOTE: in case of using unsecured network, it will be difficult to publish your app on Android & IOS.

Ujjwal Raijada
  • 867
  • 10
  • 21