0

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): 
Fozuda
  • 61
  • 1
  • 8
  • If you get data from API refer my answer [here](https://stackoverflow.com/a/68709502/13997210) or [here](https://stackoverflow.com/a/68533647/13997210) or [here](https://stackoverflow.com/a/68594656/13997210) or [here](https://stackoverflow.com/a/69116765/13997210) hope it's helpful to you – Ravindra S. Patil Sep 10 '21 at 15:44

1 Answers1

2

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.

Peter Koltai
  • 8,296
  • 2
  • 10
  • 20
  • 1
    Welcome. Looking deeper, the root cause is that the URL with the token is too long. The error comes from an `INSERT` into some kind of a log file that wants to store URL, which contains token. With 1046 characters long, it can't be inserted into the database field. – Peter Koltai Sep 10 '21 at 16:09
  • Please see this (link below) problem. i think this problem also form backend! and json format problem am i right? https://stackoverflow.com/questions/69133085/cant-understand-http-request-problem-though-it-works-fine-last-7-days-ago-in-fl – Fozuda Sep 10 '21 at 16:40
  • 1
    That is exactly the same. SQL fails and injects an error message into output JSON, making it invalid. – Peter Koltai Sep 10 '21 at 17:06
  • Thanks. but it happened for long URL what you say. its true. but if i want to make request using... Future fetchAlbum() async { final response = await http.get( Uri.parse('https://jsonplaceholder.typicode.com/albums/1'), headers: { HttpHeaders.authorizationHeader: 'Basic your_api_token_here', }, ); final responseJson = jsonDecode(response.body); return Album.fromJson(respons } it don't work. but if i use query parameter then it work, like "url?token=", so, if i want to make my url smaller using above code, its says "Undefined index: token" – Fozuda Sep 10 '21 at 17:20
  • so, i tried in many ways, finally it works fine when i send token in url using query parameter. but in header i send token using HttpHeaders.contentTypeHeader or HttpHeaders.authorizationHeader. also tried 'Authorization': 'token', everything fails. so, what is the good solution? thanks peter again. you helped me many times... – Fozuda Sep 10 '21 at 17:28
  • 1
    If the backend requires `token` int the URL query string, and you can't get a shorter token, you can' t really solve this. It tries to save to a log table but the URL column has not enough characters to store longer URLs. – Peter Koltai Sep 10 '21 at 17:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/236981/discussion-between-fozuda-and-peter-koltai). – Fozuda Sep 10 '21 at 17:42