在服务器端的Dart中,如何在HttpClient中设置头 [英] In Dart on server-side, how to set headers in HttpClient
问题描述
我试图使用Dart HttpClient类(io库/服务器端!),我不能想象如何做Dart(客户端)调用setRequestHeader的EQUIVALENT。
具体来说,我想按照此行将内容类型设置为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编辑器中)head不是请求的方法...
我在API中看到它?!)这可能与它使用
作为POST相关吗?
提前感谢!
Guenter Zoechbauer与我离线工作,离线获取这些骰子,我找不到任何例子1.服务器端,2. POST 3. HTTP命令使用标题,这种格式允许这个...
< pre class =lang-dart prettyprint-override>
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);
}
I am trying to use the Dart HttpClient class (io library / server-side!) and I can not think how to do the EQUIVALENT of the Dart (client-side) call to setRequestHeader.
Specifically I want to set "Content-type" to "application/json"
as per this line (from Client-side):
request.setRequestHeader("Content-type", "application/json");
I'm using the format:
new HttpClient().postUrl(Uri.parse(url))
.then((HttpClientRequest request) => request.close())
.then((HttpClientResponse response) => response.transform(new Utf8Decoder()).listen(_set_dbStats));
and when I try to insert:
.then((HttpClientRequest request) => request.head("Content-type", "application/json"))
I'm informed (in the Dart editor) that head is not a method for request... (although I see it in the API?!) Could this be related to it being used as a POST?
Thanks in advance!
Here is a snippet of working code that evolved from numerous tests and dealing with some issues that were outside the scope of the original question.
Guenter Zoechbauer heroically worked with me off-line to get this dones, I could not find any examples of 1. Server Side, 2. POST 3. HTTP commands using Headers, this format permits this...
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);
}
这篇关于在服务器端的Dart中,如何在HttpClient中设置头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!