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

查看:10
本文介绍了在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天全站免登陆