Comments (13)
If you are using prereleases of v2.0.0+beta.1
or above, then the API has changed a bit. Check out the guides for 2.0.0 if you want to know how much has changed.
In any case, here's an example of how to set headers with the new API:
class WeatherApiInterceptor extends InterceptorContract {
@override
Future<BaseRequest> interceptRequest({required BaseRequest request}) async {
final cache = await SharedPreferences.getInstance();
final Map<String, String>? headers = Map.from(request.headers);
headers?[HttpHeaders.contentTypeHeader] = "application/json";
return request.copyWith(
url: request.url.addParameters({
'appid': cache.getString(kOWApiToken) ?? '',
'units': 'metric',
}),
headers: headers,
);
}
@override
Future<BaseResponse> interceptResponse(
{required BaseResponse response}) async =>
response;
}
from http_interceptor.
Also, thank you for opening an issue, I appreciate you reaching out! 😄
from http_interceptor.
hello sir thx for quick reply as you sujested I had changed my interceptor but the result is same
you can see in the image that the access token which I am printing and accesstoken which I am refreshing using refresh token is same but headers not updating
class AuthorizationInterceptor extends InterceptorContract {
@override
Future<BaseRequest> interceptRequest({required BaseRequest request}) async {
final prefs = await SharedPreferences.getInstance();
final extractData =
json.decode(prefs.getString('userData')!) as Map<String, dynamic>;
final Map<String, String> headers = Map.from(request.headers);
headers['Authorization'] = await extractData['accessToken'];
print(
'this is from AuthorizationInterceptor: ${extractData['accessToken']}');
// TODO: implement interceptRequest
return request.copyWith(
headers: headers,
);
}
from http_interceptor.
Taking a closer look to your retry policy (the one sent via email) I've noticed one thing: you are not returning true
when you are refreshing your token.
class ExpiredTokenRetryPolicy extends RetryPolicy {
BuildContext context;
ExpiredTokenRetryPolicy(this.context);
@override
int get maxRetryAttempts => 2;
@override
Future<bool> shouldAttemptRetryOnResponse(BaseResponse response) async {
if (response.statusCode == 401) {
print('retry token started');
await Provider.of<Auth>(context, listen: false).restoreAccessToken();
}
return false;
}
}
This is needed because otherwise the RetryPolicy
will not know when to perform retries on response, and thus the requests will still use the 'old' token. Here's the example ExpiredTokenRetryPolicy
from the WeatherApp
provided with the library:
class ExpiredTokenRetryPolicy extends RetryPolicy {
@override
int get maxRetryAttempts => 2;
@override
Future<bool> shouldAttemptRetryOnException(
Exception reason,
BaseRequest request,
) async {
log(reason.toString());
return false;
}
@override
Future<bool> shouldAttemptRetryOnResponse(BaseResponse response) async {
if (response.statusCode == 401) {
log('Retrying request...');
final cache = await SharedPreferences.getInstance();
cache.setString(kOWApiToken, kOpenWeatherApiKey);
// Notice how in here the policy returns true so that the library can now
// when to perform a retry. In this case it is when the response's status
// code is 401 Unauthorized.
return true;
}
return false;
}
}
from http_interceptor.
hello sir as you said I had kept return true but I am facing exception if I get 401 response
from http_interceptor.
This seems like a bug in the beta version. I think it was mentioned in an issue before. Will take a look and get back to you 👀
from http_interceptor.
By any chance are you using a MultipartRequest
to upload a file (e.g. upload profile picture to a server)?
from http_interceptor.
yeah
from http_interceptor.
This seems like a bug in the beta version. I think it was mentioned in an issue before. Will take a look and get back to you 👀
thank you waiting for update
from http_interceptor.
hello sir may I know approximate next release date
from http_interceptor.
Hey, yes, hum in terms of ETA I can't give you one, but I can confirm at the moment that it is a bug. I'm working to fix this but it might need a big refactor.
from http_interceptor.
ok thank you
from http_interceptor.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
from http_interceptor.
Related Issues (20)
- Exclude specific calls from being intercepted/retried -- HeaderSanitizerContract proposal HOT 9
- Unhandled Exception: FormatException: Unexpected extension byte (at offset 1) HOT 3
- Retry Policy with delay HOT 6
- How to test http client with interceptor? TimeoutException after ... Future not completed HOT 7
- HandshakeException HOT 8
- It is possible to decode all responses to UTF-8 using interceptResponse? HOT 7
- Body is set to empty string when it should be null HOT 4
- Using interceptors on multipart request HOT 9
- Ability to edit Response body in interceptor HOT 5
- How to use with WebSockets HOT 1
- Upgrade http dependency to 1.0.0 HOT 2
- Problem while logging request and response HOT 7
- type 'Response' is not a subtype of type 'StreamedResponse' in type cast
- refreshToken called multiple times HOT 1
- How should i handle shouldInterceptRequest when you have interceptRequest HOT 1
- interceptRequest method HOT 3
- Why is no new version on pub.dev? HOT 2
- Release version HOT 4
- Allow 'maxRetryAttempts' to be flexible according to response status HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from http_interceptor.