飞镖:http发布上传到Google云端硬盘 [英] Dart: http post upload to google Drive

查看:100
本文介绍了飞镖:http发布上传到Google云端硬盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用命令行curl命令成功上传一个文件之后,我尝试将文件上传到Dart的Google Drive:

I try to upload a file to google Drive in Dart after successful upload one with a command line curl command:

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"






飞镖版本:


the dart version:

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驱动器发送文件http请求吗?

Can someone help me to send a file http request to google drive in Dart ?

推荐答案

要将更复杂的表单项添加到 MultipartRequest 时,您可以将其添加为文件。 (是的,有点奇怪...)

When you want to add a more complex form item to MultipartRequest, you add it as a file. (Yes, slightly strange...)

用...替换 request.fields.add ...

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

这篇关于飞镖:http发布上传到Google云端硬盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆