dart - Dart 中的 curl 功能

标签 dart

我正在寻找在 Dart 中获得类似 curl 功能的最佳方法。例如,如何获取 google.com 网络内容并输出它,作为示例。

我发现我可以通过 the shell as shown here 调用它,但这似乎不是理想的方法:

import 'dart:io';

main() {
  var f = new File(new Options().executable);
  Process.start('curl', 
                ['--dump-header', '/tmp/temp_dir1_M8KQFW/curl-headers', '--cacert',
                 '/Users/ager/dart/dart/third_party/curl/ca-certificates.crt', '--request', 
                 'POST', '--data-binary', '@-', '--header', 'accept: ', '--header', 'user-agent: ' ,
                 '--header', 'authorization: Bearer access token', '--header', 
                 'content-type: multipart/form-data', '--header',
                 'content-transfer-encoding: binary', '--header',
                 'content-length: ${f.lengthSync()}', 'http://localhost:9000/upload']).then((p) {
    f.openInputStream().pipe(p.stdin);
    p.stdout.pipe(stdout);
    p.stderr.pipe(stderr);
    p.onExit = (e) => print(e);
  });
}

我还查看了 API,在这里找不到任何可以帮助我的东西。

最佳答案

Dart IO 库带有 HttpClient 这基本上就是你要找的。但是,您可能应该使用 http改为发布包。将其添加到您的依赖项文件中:

dependencies:
  http: any

运行 pub install然后只是:

import 'package:http/http.dart' as http;

main() {
  http.read('http://google.com').then((contents) {
    print(contents); // Here we output the contents of google.com.
  });
}

关于dart - Dart 中的 curl 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14013069/

相关文章:

dynamic - Dart 2中的动态类型可以分配给差异类型的多个值吗?

flutter/Dart :How to extract email and phone number from a string in Flutter

constructor - 从Dart中的工厂构造函数返回null是否可以接受?

dart - 为什么不能将Iterable <DeclarationMirror>强制转换为Iterable <MethodMirror>?

django - Flutter 向 Django API 发送一个以文件为正文的 POST 请求

dart - 在 flutter 中为 MapView 设置高度

flutter - Flutter-显示框/菜单以及TextField上的列表更改事件

dart - 有什么更好的方法来处理环境之间不同的服务URL?

dart - '??'运算符有什么用

dart - Dart ClipboardData始终为null