flutter使用http库进行post请求文件上传返回400
阿里云oss明确要求form请求开头必须是以Content-Disposition: form-data; name='*'开头,而在上传文件的时候,http库在添加_headerForFile方法的时候,将Content-Type: ${file.contentType}放在了Content-Disposition之前,所以导致oss认为请求格式不正确,修改他们的位置顺序之后请求就回复正常了。
修改之前:
/// Returns the header string for a file.
///
/// The return value is guaranteed to contain only ASCII characters.
String _headerForFile(MultipartFile file) {
var header = 'Content-Type: ${file.contentType}\r\n'
'Content-Disposition: form-data; name='${_browserEncode(file.field)}'';
if (file.filename != null) {
header = '$header; filename='${_browserEncode(file.filename!)}'';
}
return '$header\r\n\r\n';
}
修改之后:
/// Returns the header string for a file.
///
/// The return value is guaranteed to contain only ASCII characters.
String _headerForFile(MultipartFile file) {
var header =
'Content-Disposition: form-data; name='${_browserEncode(file.field)}'\r\n'
'Content-Type: ${file.contentType}';
if (file.filename != null) {
header = '$header; filename='${_browserEncode(file.filename!)}'';
}
return '$header\r\n\r\n';
}
赞0
踩0