gpt4 book ai didi

http - 在服务器端的 Dart 中,如何在 HttpClient 中设置 header

转载 作者:可可西里 更新时间:2023-11-01 16:11:12 26 4
gpt4 key购买 nike

我正在尝试使用 Dart HttpClient 类(io 库/服务器端!),但我想不出如何执行 Dart(客户端)对 setRequestHeader 调用的等效操作。

具体来说,我想将“Content-type”设置为“application/json”

按照这一行(来自客户端):

request.setRequestHeader("内容类型", "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 中看到了它?!)这可能与它的使用有关作为一个职位?

提前致谢!

最佳答案

这是一段工作代码,是从大量测试和处理超出原始问题范围的一些问题演变而来的。

Guenter Zoechbauer 英勇地与我一起离线完成这项工作,我找不到任何示例 1. 服务器端,2. POST 3. 使用 header 的 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);
}

关于http - 在服务器端的 Dart 中,如何在 HttpClient 中设置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23967250/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com