我正在尝试使用Dart HttpClient类(io库/服务器端!),我无法想到如何对set(与客户端)调用setRequestHeader进行EQUIVALENT。
具体来说,我想设置" Content-type"到" application / json"
根据此行(来自客户端):
request.setRequestHeader(" Content-type"," application / json");
我使用的格式为:
new HttpClient().postUrl(Uri.parse(url))
.then((HttpClientRequest request) => request.close())
.then((HttpClientResponse response) => response.transform(new Utf8Decoder()).listen(_set_dbStats));
当我尝试插入时:
.then((HttpClientRequest request) => request.head("Content-type", "application/json"))
我告知(在Dart编辑器中)头部不是请求的方法...... (虽然我在API中看到它?!)这可能与它的使用有关 作为POST?
提前致谢!
答案 0 :(得分:2)
以下是一系列工作代码,这些代码是从众多测试中演变而来处理一些超出原始问题范围的问题。
Guenter Zoechbauer英勇地与我离线工作以获得这些dones,我找不到任何1.服务器端的示例,2。POST 3.使用Headers的HTTP命令,此格式允许此...
import 'package:http/http.dart' as http;
String url = "https://whatever.com";
return new http.Client()
.post(url, headers: {'Content-type': 'application/json'},
body: '{"distinct": "users","key": "account","query": {"active":true}}')
.whenComplete(() => print('completed'))
.then((http.Response r) => r.body);
}
答案 1 :(得分:1)
import 'package:http/http.dart' as http;
new http.Client().head(headers will go here)