Dart发送GET请求返回空数组,Postman可正常获取数据求助
Dart GET请求返回空数组但Postman正常的问题排查
我是Dart新手,遇到一个奇怪的问题——用Dart发送GET请求时返回空数组,但用Postman发送完全相同的请求却能正常获取数据。我尝试模仿Postman生成的代码调试,但问题依然存在,恳请帮忙排查两者的差异原因。
我的Dart代码
void loadData() async { showLoading(context); try { hideDialog(context); // String url = 'http://127.0.0.1:5000'; String url = '127.0.0.1:5000'; String endpoint = '/carrier_loadboard'; String option = 'Market'; String orgId = '2'; final queryParameters = { 'option': option, 'org_id': orgId, }; final headers = { 'Authorization': 'Bearer$userToken', 'Content-Type': 'application/json', 'Cookie': 'session=$sessionCookie', }; var creds = json.encode({ 'email': 'carrier@domain.com', 'password': 'thePassword', }); final uri = Uri.http(url, endpoint, queryParameters); final response = await get(uri, headers: headers); // var request = Request( // 'GET', // Uri.parse( // 'http://127.0.0.1:5000/carrier_loadboard?option=Market&org_id=2')); // request.body = creds; // request.headers.addAll(headers); // StreamedResponse response = await request.send(); if (response.statusCode == 200) { // print(await response.stream.bytesToString()); print('Load data success'); List data = json.decode(response.body); print(data); if (data.isEmpty) { print('No data'); } } else { print(response.reasonPhrase); print('Load data failed'); } } catch (e) { devlog.log(e.toString()); } }
Postman生成的可正常运行代码
var headers = { 'Authorization': 'Bearer tokenTokenToken', 'Content-Type': 'application/json', 'Cookie': 'session=.cookieCodeCookieCode' }; var request = http.Request('GET', Uri.parse('http://127.0.0.1:5000/carrier_loadboard?option='Market'&org_id='2'')); request.body = '''// {"email": "carrier@domain.com", "password":"thePassword"}\n'''; request.headers.addAll(headers); http.StreamedResponse response = await request.send(); if (response.statusCode == 200) { print(await response.stream.bytesToString()); } else { print(response.reasonPhrase); }
响应结果对比
- Dart请求响应结果:
[]
- Postman请求响应结果(仅展示第一个对象):
[ { "agent_id": null, "assigned_carrier_id": null, "carrier_org_id": 2, "commodity": null, "deliver_address": "700 4th St, San Francisco, CA 94107, USA", "deliver_appt": null, "deliver_appt_required": false, "deliver_date": "2021-10-22T17:46:00", "deliver_from_date": "2022-03-12T00:00:00", "deliver_to_date": null, "driver_id": null, "expected_delivery_bid": "1000", "expected_posting_bid": "1300", "id": 104, "load_num_broker": null, "load_num_carrier": null, "load_num_shipper": null, "margin": null, "notes": null, "office_hr_end": null, "office_hr_start": null, "org_created": null, "org_id_posted": 1, "org_id_reposted": 1, "org_name": null, "pickup_address": "801 Alaskan Way, Seattle, WA 98104, USA", "pickup_appt": null, "pickup_appt_required": false, "pickup_date": "2021-10-18T17:46:00", "pickup_from_date": "2022-03-12T00:00:00", "pickup_to_date": null, "po_num": null, "poster_id": null, "shipper_id": null, "status": "Completed", "time_accepted": null, "time_posted": "2021-10-19T00:47:24.060521", "trailer_id": null, "trailer_type": "Double", "truck_id": "", "weight": "1000" } ]
排查要点
- Authorization头的空格问题:你的Dart代码中
Authorization头是'Bearer$userToken',而Postman里是'Bearer tokenTokenToken'——注意Bearer和token之间有一个空格。服务器解析token时,可能会把Bearerxxx当成无效token,导致权限验证不通过,返回空数组。把Dart代码里的这行改成'Bearer $userToken'(加空格)试试。 - GET请求的body差异:Postman的代码给GET请求添加了body(包含邮箱密码),但标准HTTP规范中GET请求通常不携带body,不过有些服务器会特殊处理这种情况。你当前用Dart的
get方法发送请求,这个方法不支持设置body;而你注释掉的Request方式是可以设置body的,和Postman的逻辑一致。可以尝试启用注释的代码,用Request方式发送请求,看是否能拿到数据。 - 确认Token和Cookie的正确性:检查
userToken和sessionCookie的实际值是否和Postman中使用的完全一致,有没有在拼接时出现截断、拼写错误等问题。比如session=$sessionCookie是否正确拼接了完整的cookie值。 - URL验证:虽然
Uri.http('127.0.0.1:5000', ...)生成的URL应该和Postman的一致,但可以打印uri.toString()确认最终请求的URL是否和Postman里的完全相同,避免query参数拼接错误。
内容的提问来源于stack exchange,提问作者KurtReyn
相关产品推荐
相关产品推荐

