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

3
  • use: Uri.parse('http://product.acirfa.tech:3000/products/224213463241') Commented Apr 26, 2021 at 4:45
  • I have already tried to put it directly in the http.get() but didn't work. Thanks for the suggestion ! Commented Apr 26, 2021 at 4:59
  • Might be it is because of Android 9 or upper version you should try stackoverflow.com/questions/51902629/… Commented Apr 26, 2021 at 5:13

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.