Best way to set default header for all request in flutter http request

前端 未结 5 1611
臣服心动
臣服心动 2021-02-04 02:04

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 fun

5条回答
  •  遥遥无期
    2021-02-04 03:01

    This can be easily made with the Dio package.

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

    Update

    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.

提交回复
热议问题