英文:
Fail to get http response with form-data
问题
无法从Flutter中的HTTP POST请求中获取响应,但URL和数据已在Postman中验证。
var map = new Map<String, String>();
final url = Uri.parse(globals.ServerDomain + '/login');
Map<String, String> requestBody = <String, String>{
'username': '80889099',
'password': '123456789abcde'
};
var request = http.MultipartRequest('POST', url)
..fields.addAll(requestBody);
var response = await request.send();
final respStr = await response.stream.bytesToString();
print(respStr);
没有返回结果。
英文:
I am not able to get response from http POST in Flutter but URL and data are verified in Postman.
var map = new Map<String, String>();
final url = Uri.parse(globals.ServerDomain + '/login');
Map<String, String> requestBody = <String, String>{
'username': '80889099',
'password': '123456789abcde'
};
var request = http.MultipartRequest('POST', url)
..fields.addAll(requestBody);
var response = await request.send();
final respStr = await response.stream.bytesToString();
print(respStr);
no result returned.
答案1
得分: 2
Flutter有一个很棒的包叫做Dio,用来处理各种HTTP请求。使用它非常容易实现你想要的功能,如果你正在使用表单数据,那么这就是你应该使用的方法。要了解更多详情,请查看这个链接:https://pub.dev/packages/dio#sending-formdata
示例代码:
final formData = FormData.fromMap(
{'username': '80889099',
'password': "123456789abcde",
});
final response = await dio.post('${globals.ServerDomain}/login', data: formData);
英文:
Flutter has this great package called Dio to handle all sort of http requests. It's very easy to do what you want with it, you are using form data so this is what you should use. For more details check this https://pub.dev/packages/dio#sending-formdata
Example code:
final formData = FormData.fromMap(
{'username': '80889099',
'password': "123456789abcde",
});
final response = await dio.post('${globals.ServerDomain}/login', data: formData);
答案2
得分: 0
看起来这是一个大型项目,更改包可能不是一个好选择,尝试添加请求头
'Content-Type': 'multipart/form-data'
示例代码:
Map<String, String> headers = <String, String>{
'Authorization': 'Basic ${base64Encode(utf8.encode('user:password'))}',//your any other header
'Content-Type': 'multipart/form-data'
};
var map = new Map<String, String>();
final url = Uri.parse(globals.ServerDomain + '/login');
Map<String, String> requestBody = <String, String>{
'username': '80889099',
'password': '123456789abcde'
};
var request = http.MultipartRequest('POST', URL)
..headers.addAll(headers)
..fields.addAll(requestBody);
var response = await request.send();
final respStr = await response.stream.bytesToString();
print(respStr);
此外,请检查是否已授予互联网权限(这可能不是问题的原因,只是提供信息)。
英文:
Looks like Its big project changing the package is not a good option try adding request header
'Content-Type': 'multipart/form-data'
example code :
Map<String, String> headers= <String,String>{
'Authorization':'Basic ${base64Encode(utf8.encode('user:password'))}',//your any other header
'Content-Type': 'multipart/form-data'
};
var map = new Map<String, String>();
final url = Uri.parse(globals.ServerDomain + '/login');
Map<String, String> requestBody = <String, String>{
'username': '80889099',
'password': '123456789abcde'
};
var request = http.MultipartRequest('POST', URL)
..headers.addAll(headers)
..fields.addAll(requestBody);
var response = await request.send();
final respStr = await response.stream.bytesToString();
print(respStr);
Additionally check if internet permission is given (may not be the cause just info)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论