我正在尝试将数据发布到服务器,并且成功从服务器获得了真实的响应,但是我发布的项目没有反映在服务器中。但是,当我对邮递员执行相同的请求时,它将被发布并在服务器中得到反映。因此,在检查了其中一些内容后,我发现从邮递员请求端开始,Content-Type,Content-Length和Host是必需的,但我只是在代码中实现了Content-Type。因此,尝试一下,我想如果我添加Content-Length和Host,它可能会反映在服务器中,但是如何获取该数据?
下面是我的代码
var uri = Uri.parse(Constant.postAdUrl);
Map<String, String> headers = {
'Content-Type': 'multipart/form-data',
"Accept": "application/json",
'Content-Length' : ''
};
http.MultipartRequest request = new http.MultipartRequest('POST', uri);
request.headers.addAll(headers);
List<http.MultipartFile> newList = new List<http.MultipartFile>();
for (int i = 0; i < images.length; i++) {
var path =
await FlutterAbsolutePath.getAbsolutePath(images[i].identifier);
File imageFile = File(path);
var streams = new http.ByteStream(imageFile.openRead());
var lengths = await imageFile.length();
request.files.add(await new http.MultipartFile(
'files[]', streams, lengths,
filename: _image.path));
}
if (_image != null) {
request.files.add(await new http.MultipartFile('adcover', stream, length,
filename: _image.path));
}
request.files.addAll(newList);
request.fields['ads_userfkid'] = userId;
request.fields['ads_name'] = widget.title;
request.fields['ads_description'] = widget.description;
request.fields['ads_price'] = widget.price;
request.fields['ads_subcatfkid'] = widget.subId;
request.fields['ads_location'] = widget.location;
var response = await request.send();
if (response.statusCode == 200) {
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => DashboardScreen()));
} else {
print("Upload Failed");
}
}