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): 
1
  • If you get data from API refer my answer here or here or here or here hope it's helpful to you Commented Sep 10, 2021 at 15:44

1 Answer 1

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.

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

7 Comments

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.
Please see this (link below) problem. i think this problem also form backend! and json format problem am i right? stackoverflow.com/questions/69133085/…
That is exactly the same. SQL fails and injects an error message into output JSON, making it invalid.
Thanks. but it happened for long URL what you say. its true. but if i want to make request using... Future<Album> fetchAlbum() async { final response = await http.get( Uri.parse('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"
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.