是的,所以我一直在做一些需要通过标头进行基本身份验证,并通过HTTP Post传递一些变量的工作.这是一个终端应用程序.

我的代码如下所示:

import 'package:http/http.dart' as http;
import 'dart:io';
void main() {
  var url = "http://httpbin.org/post";
  var client = new http.Client();
  var request = new http.Request('POST', Uri.parse(url));
  var body = {'content':'this is a test', 'email':'john@doe.com', 'number':'441276300056'};
  request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json; charset=utf-8';
  request.headers[HttpHeaders.AUTHORIZATION] = 'Basic 021215421fbe4b0d27f:e74b71bbce';
  request.body = body;
  var future = client.send(request).then((response) => response.stream.bytesToString().then((value) => print(value.toString()))).catchError((error) => print(error.toString()));
}

我使用httpbin作为回音服务器,所以它告诉我要传递什么.如果我没有传递主体,或者如果我传递字符串作为主体,我的代码可以正常工作.

显然,这是因为http中的body属性.请求只接受字符串,我正在try 向它传递一个映射.

我可以将其转换为字符串,这可能会起作用,但我仍然认为我的代码可以改进.不是从语法的Angular 来看,也不是从它如何处理future 的Angular 来看,但是我不确定使用http.dart是不是正确的做法.

有人能给我指个方向吗?

提前谢谢.

推荐答案

JSON is是一个字符串.您需要将映射编码为JSON,并将其作为字符串传递.

您可以使用bodyFields而不是body传递映射.
这样你的content-type就固定到"application/x-www-form-urlencoded"了.

post年度达特博士说:

/// If [body] is a Map, it's encoded as form fields using [encoding]. The
/// content-type of the request will be set to
/// `"application/x-www-form-urlencoded"`; this cannot be overridden.

不久前我可以通过这种方式发送JSON数据

return new http.Client()
  .post(url, headers: {'Content-type': 'application/json'},
      body: JSON.encoder.convert({"distinct": "users","key": "account","query": {"active":true}}))
      .then((http.Response r) => r.body)
      .whenComplete(() => print('completed'));

EDIT

import 'package:http/http.dart' as http;
import 'dart:io';
void main() {
  var url = "http://httpbin.org/post";
  var client = new http.Client();
  var request = new http.Request('POST', Uri.parse(url));
  var body = {'content':'this is a test', 'email':'john@doe.com', 'number':'441276300056'};
//  request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json; charset=utf-8';
  request.headers[HttpHeaders.AUTHORIZATION] = 'Basic 021215421fbe4b0d27f:e74b71bbce';
  request.bodyFields = body;
  var future = client.send(request).then((response)
      => response.stream.bytesToString().then((value)
          => print(value.toString()))).catchError((error) => print(error.toString()));
}

生产

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "content": "this is a test", 
    "email": "john@doe.com", 
    "number": "441276300056"
  }, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Authorization": "Basic 021215421fbe4b0d27f:e74b71bbce", 
    "Connection": "close", 
    "Content-Length": "63", 
    "Content-Type": "application/x-www-form-urlencoded; charset=utf-8", 
    "Host": "httpbin.org", 
    "User-Agent": "Dart/1.5 (dart:io)", 
    "X-Request-Id": "b108713b-d746-49de-b9c2-61823a93f629"
  }, 
  "json": null, 
  "origin": "91.118.62.43", 
  "url": "http://httpbin.org/post"
}

Dart相关问答推荐

flutter.io (dart) - 将填充设置为设备宽度的百分比?

Flutter:是否可以有多个 Futurebuilder 或一个 Futurebuilder 用于多个 Future 方法?

如何防止意外发布私有 pub 包

在 Dart 中,List.unmodifiable() 和 UnmodifiableListView 有什么不同?

如何防止键盘在Flutter中按提交键时失效?

如何在仍然引用来自 pub.dartlang.org 的包的同时运行私有 pub 服务器?

Flutter 屏幕尺寸计算

Dart/Flutter 如何在 map 中初始化列表?

Flutter有向图.我可以将CustomPainter类与自定义小部件一起使用吗?

谷歌dart Regions?

不推荐使用AnteStorStateofType,请改用findAncestorStateOfType

Flutter: shared preferences

Dart 中的snapshot快照概念是什么?

dart 中隐式转换运算符的语法是什么?

列表的反向迭代器?

Dart vs Polymer vs Bootstrap

将多张map合并/合并为一张map

Dart 中如何将列表转换为map

Angular.js 和 Angular.dart 的区别?

了解工厂构造函数代码示例