问题
I have to pass token, received from server, after login to every api. so my question is how to write an interceptor which will append token on every api call. I am using 'request-promise' to call api.
Thanks!
回答1:
I would create a wrapper (interface) function for your 'request-promise' calls:
export default class RequestInterface {
constructor(accessToken) {
this.accessToken = accessToken;
}
request({
method,
uri,
body,
}) {
return rp({
method,
uri,
body,
qs: {
access_token: this.accessToken,
},
});
}
}
This way on every RequestInterface.request() call, you'll have your credentials there.
(I didn't test this but that's the basic idea)
来源:https://stackoverflow.com/questions/40205707/how-to-write-interceptor-in-react-redux