为Flutter HTTP请求中的所有请求设置默认标头的最佳方法 [英] Best way to set default header for all request in flutter http request

查看:441
本文介绍了为Flutter HTTP请求中的所有请求设置默认标头的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这个问题如此基本,但是我不熟悉,最近又找不到在HTTP请求中设置默认标头的好方法,我可以扩展该类或为其包装一个函数,但是不应该有一个简单的内置方法,但是我在文档中找不到。

Sorry if this question so basic, but I am new to flutter and recently couldn't find a good way to set a default headers in the HTTP request I can extend the class or wrap a function to it but shouldn't it there be an easy way that is build-in but I couldn't find it in the documentation.

推荐答案

这可以通过Dio软件包轻松完成。

This can be easily made with the Dio package.

https://pub.dartlang.org/packages/dio

更新

基于新的Dio API:

Based on the new Dio API:

var dio = Dio();
dio.interceptors.add(InterceptorsWrapper(onRequest: (RequestOptions options) async {
  var customHeaders = {
    'content-type': 'application/json'
    // other headers
  };
  options.headers.addAll(customHeaders);
  return options;
}));

Response response = await dio.get("url");
print(response.data.toString());

请参考文档以获取更多详细信息。

Refer the documentation for more details.

这篇关于为Flutter HTTP请求中的所有请求设置默认标头的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆