i try to call api using generated dart code from postman. the link i put on flutter and postman is same link but only postman work.
import 'package:flutter/material.dart';
import 'dart:async' show Future;
import 'dart:convert';
import 'package:http/http.dart' as http;
class DartAPI {
Future getlist() async{
var headers = {
'Ocp-Apim-Subscription-Key': 'thekeyishere'
};
var request = http.Request('GET', Uri.parse('https://linkishere.com/all?page=1'));
request.headers.addAll(headers);
http.StreamedResponse response = await request.send();
if (response.statusCode == 200) {
print(await response.stream.bytesToString());
}
else {
print(response.reasonPhrase);
}
}
}
and the error i got is
'Failed Host To lookup:https://linkishere.com/all?page=1'
even i have internet on my physical devices.
how do i call api from flutter and get the same result as postman
Try this code, please!
import 'package:http/http.dart' as http;
Future<void> getlist() async {
try {
var headers = {
'Ocp-Apim-Subscription-Key': 'thekeyishere',
};
var uri = Uri.parse('https://linkishere.com/all?page=1');
var response = await http.get(uri, headers: headers);
if (response.statusCode == 200) {
print(response.body);
} else {
print(response.reasonPhrase);
}
} catch (e) {
print('Error occurred: $e');
}
}