Dart:http post上传到谷歌硬盘
在使用命令行curl命令成功上载文件后,我尝试将文件上载到Dart中的google Drive:Dart:http post上传到谷歌硬盘,dart,flutter,Dart,Flutter,在使用命令行curl命令成功上载文件后,我尝试将文件上载到Dart中的google Drive: curl -X POST -L \ -H "Authorization: Bearer $access_token" \ -F "metadata={name : 'test_send_file.zip'};type=application/json;charset=UTF-8" \ -F "file=@test_send_file.zip;type=application/
curl -X POST -L \
-H "Authorization: Bearer $access_token" \
-F "metadata={name : 'test_send_file.zip'};type=application/json;charset=UTF-8" \
-F "file=@test_send_file.zip;type=application/zip" \
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
dart版本:
import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';
final access_token = "....";
main() async {
File first = File('test_send_file.zip');
Uri uri = Uri.parse(
'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart');
http.MultipartRequest request = new http.MultipartRequest('POST', uri);
request.headers["Authorization"] = "Bearer $access_token";
request.fields['metadata'] =
"{name : test_send_file.zip};type=application/json;charset=UTF-8";
request.files.add(await http.MultipartFile.fromPath('test_send_file.zip', first.path,
contentType: new MediaType('application', 'zip')));
print("request.toString: " + request.toString());
http.StreamedResponse response = await request.send();
print(response.statusCode);
}
我收到一个状态代码错误请求:
400
有人能帮我向google drive in Dart发送一个文件http请求吗?当你想向
多部分请求添加一个更复杂的表单项时,你可以将其作为文件添加。(是的,有点奇怪……)
替换请求.字段。将…
添加为
request.files.add(http.MultipartFile.fromString(
'metadata',
json.encode({'name': 'test_send_file.zip'}),
contentType: MediaType('application', 'json'),
));
谢谢你,这很有效!我已将request.fields['metadata']..
和request.files.add…
替换为request.files.add.
已被此问题困扰。这是行不通的。这个名字总是没有名字。似乎元数据需要作为多部分/相关内容类型传递,但我得到了400