简体   繁体   中英

Flutter: Send JSON body with dio package get request

此 API 在邮递员中正常工作 however in flutter app its returning this

{"success": true, "rslt": []}

where "rslt" is empty.

here's my code

dynamic getData(dynamic token) {
dio.options.headers['Authorization'] = '$token';
return await dio.get<dynamic>('https://address',
    queryParameters: <String,dynamic>{'cn': 'iPhone_11', 'qt': '20', 'ct': 'Delhi'});
}

What am I doing wrong in this code?

In postman, you have added body to GET request. Normally GET request should not have body. But there is no body in your request in Flutter .

You pass data with parameters but need to pass in body section that was missing, you need to call like that as below


Map data = {'cn': 'iPhone_11', 'qt': '20', 'ct': 'Delhi'};
dynamic getData(dynamic token) {
dio.options.headers['Authorization'] = '$token';
return await dio.get<dynamic>('https://address',
    data: jsonEncode(data);
}

Change dio.get to dio.request:

  dio.request<dynamic>(
     'https://address',
     queryParameters: <String,dynamic>{
     'cn': 'iPhone_11', 'qt': '20', 'ct': 'Delhi'},
     options: Options(method: "GET"));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM