【问题标题】:In Dart on server-side, how to set headers in HttpClient在服务器端的 Dart 中,如何在 HttpClient 中设置标头
【发布时间】:2014-05-31 06:45:25
【问题描述】:

我正在尝试使用 Dart HttpClient 类(io 库/服务器端!),但我想不出如何对 setRequestHeader 进行 Dart(客户端)调用的 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 编辑器中)head 不是请求方法... (尽管我在 API 中看到它?!)这可能与它正在使用有关吗 作为一个帖子?

提前致谢!

【问题讨论】:

    标签: http dart httprequest server-side


    【解决方案1】:

    这是一个工作代码的 sn-p,它从大量测试演变而来,并处理了原始问题范围之外的一些问题。

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

    【讨论】:

    • 我想向 Dart 工程师们评论一下,鉴于客户端和服务器是不同的,这会造成多么混乱。文档和示例是滞后的更改,API 文档很少包含完整的示例,而且使用 HTTP 的一般性质是,恕我直言,不是最清楚的。使用“头”还是“头”?不,哦?更不用说测试会变得非常耗时,因为很容易涉及多个问题。在此过程中,我确实看到了不止几个“堆栈溢出”结果 :)
    【解决方案2】:
    import 'package:http/http.dart' as http;
    
    new http.Client().head(headers will go here)
    

    【讨论】:

    • 我回到原来的问题帖子试图让问题更清楚。同时,我已经尝试了所有我能想到的设置标题,但没有任何效果。我认为 .headers.set() 看起来很有希望,但也没有成功。
    猜你喜欢
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2021-10-27
    • 2016-04-12
    • 1970-01-01
    相关资源
    最近更新 更多