Is it possible get error message when call rest api in flutter?
Eg.
print(response.statusCode); --return code
How print this error message?
The request could not be processed because an error occurred whilst attempting to evaluate the SQL statement associated with this resource. Please check the SQL statement is
If you are using dio package you can catch DioError and fetch a response from the error.
In my case back returns me the message in ['error'][''message].
I'm not sure how it works with http package. Firstly you can print e.response!.data to see the structure and then get the error text
try {
//make request
} on DioError catch (e, stacktrace) {
final result = e.response!.data as Map<String, dynamic>;
print(result['error']['message']); <---- getting error message from response
}
Related
I'm trying to get data from a website that hosts Weather APIs (OpenWeatherMap), but as soon as I use the get() method, I get an error that says :
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Failed host lookup: 'api.openweathermap.org'
Even tho the URL that I provided is working (I tested it, the API key is working), here is the code :
String url = "https://api.openweathermap.org/data/2.5/weather?lat=$latitude&lon=$longitude&appid=$apiKey";
// The latitude, longitude and apiKey aren't not null.
Uri uri = Uri.parse(url);
http.Response response;
try {
response = await http.get(uri);
} on Exception catch (e) {
print("Error when getting data from the url = $url"); // Im getting this line on the console,
// so the error is indeed the line above.
}
I fixed it. My phone had issues reaching Internet, so the code couldn't reach the URL provided.
I have a problem regarding fetching data or using the request API in local in flutter.
I tried to send a request using postman and it works, but when I'm trying to request in flutter, it gives me Connection refuse.
I'm using http package of flutter
This is the error, but i also tried jsonplaceholder api and it works.
I/flutter (10134): 5004/profile URI :>> http://127.0.0.1:5004/profile/v1.0/channels/1?includes=createdByProfile,joinedProfiles
I/flutter (10134): error :>> Connection refused
Heres my code:
Future loadData() async {
try {
var data = await http.get(Uri.parse('http://127.0.0.1:5000/content/v1.0/contents'));
print("data!! :>> $data");
} catch (error) {
print("ERROR! $error");
}
}
it always go to catch and says Connection Refused. But when i tried to request in postman and access the url in browser, it shows the data there
I am trying to know if the connection error is Connection to 'some URL' was not upgraded to websocket. Also identify response code. The server is emitting 401.
I need this to know if I need to refresh the token and then reconnect.
final channel = IOWebSocketChannel.connect(Url)
final sub = channel.stream.listen((data){
//process data
},
onError: (error){
//confirm this error failing to upgrade and
// response code is 401
// then refresh token and reconnect
})
To know the error type
print('error type is ${error.runtimeType}');
then you can handle it
if(error is errorType)
{
...
}
Im calling api and i got exception i want to handle that values how to access those values using dart
ApiException 401: {"errorCode":"DATA__001","values":{"param1":"Final"},"violations":null}
Ihave tried this way but no luck
var error=jsonDecode('ApiException 401: {"errorCode":"DATA__001","values":{"param1":"Final"},"violations":null}');
the http.get return a Future and that Future has an Object of Type http.Response.
and that Object has the property that name is statusCode .it store your response Code from the Server.
that is an example of how you can catch the code and work with a response as you like.
Future<http.Response> response = http.get('your api url');
response.then((http.Response responseData) {
if (responseData.statusCode==200) {
//do somthing
}else{
// do somthing else
}
}
My intention is to make a GET request using the DIO or any similar HTTP client in order to receive a JSON data/body and print it to the console.
I have written the following code to achieve that.
fetchQuestion(String userIdentifier) async {
String urlToCall =
"someURLhere";
try {
Response response = await Dio().get(
urlToCall,
options: Options(headers: {
HttpHeaders.authorizationHeader: "Bearer " + userIdentifier,
}),
);
print(response.data);
} catch (e) {
print(e);
}
}
The problem with this code is, when I print response.data, only null is printed. Even though I am certain that the response data contains a JSON file.
I have checked on the backend, and I am getting a 200 status code. Additionally, printing response.headers does print the headers I expected. It is only the response.body that prints null.
Issues I have tried include
Using print(utf8.decode(response.data));
Using json.decode(response.data) -> In which case I get
NoSuchMethodError: The getter 'length' was called on null. error.
I would appreciate any kind of help regarding printing the JSON file received.
Have you printed just response to see what fields are in there.
I haven't used DIO but http package works fine for me:
import 'package:http/http.dart' as http;
...
final response = await http.get(Url);