Fozuda
Fozuda

Reputation: 71

Api Request using Http request for get method. but don't know actual problem in Flutter

I am new in flutter and learning api request. I make a request for get method. but it shows error! i can't receive any data!

In Postman, it works fine and data comes properly!

This api and token is only for test porpuse! so, don't worry!

Api request

Future fetchAlbum() async {
  final token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjI4OTksImlzcyI6Imh0dHBzOi8vcG9ydGFsLWFwaS5qb21ha2hhdGEuY29tL2FwaS9hdXRoL2xvZ2luIiwiaWF0IjoxNjMxMjUxNjYwLCJleHAiOjE2MzEzMzgwNjAsIm5iZiI6MTYzMTI1MTY2MCwianRpIjoiNlFEUTZCYnBMT0JhdUJoaSJ9.jAY_2nYxjgsIvXZY5vn0vAr_pwF6UBYbSGZ8wqD0YPQ';
  final response = await http.get(
    Uri.parse('https://portal-api.jomakhata.com/api/getLeaveDetails?token=${token}'),
    // Send authorization headers to the backend.
  );
  final responseJson = jsonDecode(response.body);

  if(response.statusCode==200){
    print("ok");
    print(responseJson);
  }
  else{
    print("error!");
  }

  return responseJson;
}

Error in Console

D/EGL_emulation(19932): app_time_stats: avg=14700.05ms min=577.55ms max=28822.55ms count=2
E/flutter (19932): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: FormatException: Unexpected character (at character 335)
E/flutter (19932): ...ning":14}],"fiscalYear":"2021-2022"}{"message":"SQLSTATE[22001]: String ...
E/flutter (19932):                                        ^
E/flutter (19932): 
E/flutter (19932): #0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1404:5)
E/flutter (19932): #1      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:869:48)
E/flutter (19932): #2      _parseJson (dart:convert-patch/convert_patch.dart:40:10)
E/flutter (19932): #3      JsonDecoder.convert (dart:convert/json.dart:506:36)
E/flutter (19932): #4      JsonCodec.decode (dart:convert/json.dart:157:41)
E/flutter (19932): #5      jsonDecode (dart:convert/json.dart:96:10)
E/flutter (19932): #6      fetchAlbum (package:test_list/main.dart:51:24)
E/flutter (19932): <asynchronous suspension>
E/flutter (19932): 

Upvotes: 0

Views: 790

Answers (1)

Peter Koltai
Peter Koltai

Reputation: 9734

There is an error in the API endpoint you are calling, more precisely there is an SQL error SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'url' at row 1.

Because of this, an error message is inserted into the JSON output, like this:

}{
    "message": ...

This is not a problem for Postman to display, but as a result you get an invalid JSON, there should be a , between { and }.

So when you try to decode it in Flutter, you will get an error, because conversion fails: Unhandled Exception: FormatException: Unexpected character.

Upvotes: 2

Related Questions