问题描述
我目前正在从事一个项目,该项目与许多其他项目一样都使用s3存储。在这种情况下,存储是通过后端链接的。
情况是这样的,我可以通过URL获得“附件”,假设example.com/api/attachments/{uuid}
。如果用户被授权(通过标头Authorization
),则应返回302
状态码并重定向到s3 url。问题在于重定向后Authorization
标头仍然存在,并且http客户端返回400
响应,这是因为Authorization
标头一直存在。重定向后,有什么方法可以删除Authorization
标头,而不捕获第一个请求并触发新请求?
我的http客户端代码当前如下所示:
@override
Future get({
String url,Map<String,dynamic> data,String> parameters,}) async {
await _refreshClient();
try {
final response = await dio.get(
url,data: json.encode(data),queryParameters: parameters,);
return response.data;
} on DioError catch (e) {
throw ServerException(
statusCode: e.response.statusCode,message: e.response.statusMessage,);
}
}
Future<void> _refreshClient() async {
final token = await auth.token;
dio.options.baseUrl = config.baseUrl;
dio.options.headers.addAll({
'Authorization': 'Bearer $token','Accept': 'application/json',});
dio.options.contentType = 'application/json';
}
解决方法
查看 Dio 文档,这似乎是有意为之。
添加到请求中的所有标头都将添加到重定向请求中。但是,随请求发送的任何正文都不会成为重定向请求的一部分。
https://api.flutter.dev/flutter/dart-io/HttpClientRequest/followRedirects.html
但是,我理解(并同意!)这通常是不受欢迎的行为。我的解决方案是自己手动跟踪重定向,这不是很好,但在紧要关头工作。
Response<String> response;
try {
response = await dio.get(
url,options: Options(
// Your headers here,which might be your auth headers
headers: ...,// This is the key - avoid following redirects automatically and handle it ourselves
followRedirects: false,),);
} on DioError catch (e) {
final initialResponse = e.response;
// You can modify this to understand other kinds of redirects like 301 or 307
if (initialResponse != null && initialResponse.statusCode == 302) {
response = await dio.get(
initialResponse.headers.value("location")!,// We must get a location header if we got a redirect
),);
} else {
// Rethrow here in all other cases
throw e;
}
}