gpt4 book ai didi

Dart:http post 上传到 google Drive

转载 作者:IT王子 更新时间:2023-10-29 06:40:25 26 4
gpt4 key购买 nike

在使用命令行 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/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

有人可以帮我在 Dart 中向 google drive 发送文件 http 请求吗?

最佳答案

当您想向 MultipartRequest 添加更复杂的表单项时,您可以将其作为文件添加。 (是的,有点奇怪……)

request.fields.add... 替换为...

  request.files.add(http.MultipartFile.fromString(
'metadata',
json.encode({'name': 'test_send_file.zip'}),
contentType: MediaType('application', 'json'),
));

关于Dart:http post 上传到 google Drive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53768362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com