laravel - Flutter Dio 在 Laravel 服务器上发送空白请求
问题描述
颤振代码:
普通数据 JSON 对象
var data = {
'society_id': widget.societyId,
'name': _currentCategory,
'details': txtDescription.text,
'type': "Society Residential Complaint"
};
Dio 请求的 FormData
FormData formData = FormData.fromMap({
'society_id': widget.societyId,
'name': _currentCategory,
'details': txtDescription.text,
'type': "Society Residential Complaint"
});
if (selectedFile != null) {
String path = selectedFile.path;
String imagename = selectedFile.path.split('/').last;
print("Photo Selected");
formData.files.add(
MapEntry(
"photos[]",
await MultipartFile.fromFile(
path,
filename: imagename.toString(),
),
),
);
}
发出 Dio Post 请求的函数
Future raiseComplaint(FormData formData, data) async {
await _getToken();
String requestUrl = serverURL + '/complaint/add';
print("Sending Req: $requestUrl");
print("--- complaint details: $formData");
print("--- complaint details: $data");
try {
dio.options.headers['Content-Type'] = 'application/json';
dio.options.headers["Authorization"] = "Bearer $_accessToken";
Response serverResponse = await dio.post(requestUrl, data: formData);
var response = _returnResponse(serverResponse);
print("Recieving Res: $requestUrl");
print(response);
return response;
} catch (e) {
throw e;
}
}
呼叫请求
var response = await raiseComplaint(formData, data);
问题:如果我发送普通的 json 对象,从 raiseComplaint() 函数,我会收到所有参数。我也需要附件文件,所以使用 Dio FormData 对象。FormData 对象包含地图中的所有参数和fields
地图中的附件files
,我无法在服务器上接收。
在(Laravel)服务器上,由于 laravel 验证失败,我没有从请求中获取任何参数或文件。我搜索了Dio 包文档,但没有找到任何帮助。我们需要处理 Dio 请求,所以如果有人在过去遇到此错误,请提供帮助。谢谢。
解决方案
推荐阅读
- node.js - 如何在不重新启动进程(NodeJS)的情况下更新进程中的 ENV 变量?
- c - 字符串终止符问题
- r - 如何针对 R 中的分析变量绘制分类变量的条形图
- python - Django上传excel文件,用pandas处理,下载为csv
- html - 将 FlexBoxed Div 并排放置
- vb.net - 我应该如何为我的两因素身份验证系统生成“秘密”代码?
- c++ - 如何在带有向量的 C++ 中正确使用 FFTW?
- css - 如何在具有固定宽度和溢出设置为自动的 Div 中永远不会有换行
- jenkins - 尝试在 Jenkins Pipeline 中的 svn 中创建标签
- python - pydicom Sequence 类的目的是什么?