Add support for async transformRequest
See original GitHub issueHello, I have a feature request to this project. It would be great if you could add support for async transformRequest functions.
The goal I want to achieve is to add oauth2 token to the authorization header, but this token must be fetched from external service, and sometimes must be refreshed, so the function which manages it is asynchronous.
It’d be great if that code could work:
const instance = axios.create({
transformRequest: [async function (data, headers) {
headers['Authorization'] = 'Bearer ' + await loginService.getToken()
}],
});
Issue Analytics
- State:
- Created 5 years ago
- Reactions:14
- Comments:5
Top Results From Across the Web
$http.transformRequest with results from an asynchronous ...
I have an angular application that uses the $http service to fetch data from a remote server. Under certain circumstances I need to...
Read more >Request and Response Transforms
Request trailers are not supported because they are not supported by the underlying HttpClient. Adding transforms. Transforms can be added to routes either ......
Read more >xhr-async - npm
xhr-async supports request cancellation. You can abort an ongoing request, or abort a group of requests. Abort a single request. let xhrReq const ......
Read more >Using MapLibre GL JS with Amazon Location Service
Building the application: Request transformation. MapLibre GL JS map instances include a transformRequest option, which you use to intercept and modify requests ...
Read more >async function - JavaScript - MDN Web Docs - Mozilla
The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
interceptors.request
support for async function.Looks like @baijunjie has already mentioned interceptor. Sorry, I didn’t read the thread properly…
original
Hi, I don’t think the proposed solution work. Not sure why this issue is closed though…
Looking at how
transformRequest
is used intransformData
, returning a Promise insidetransformRequest
may yield incorrect result. https://github.com/axios/axios/blob/203cbc2da0ef0f68d517ba2a6176df1d48efc2fb/lib/core/transformData.js#L13-L20I don’t think
transformRequest
allow async operation out of the box.Alternative?
However, I think the requirements from both @aartek and @baijunjie can be fulfilled with Axios’ request interceptor?