Closed kinxos closed 4 years ago
Hello,
You can use hooks for that:
https://github.com/sindresorhus/ky#hooks
this.$http.get('https://example.com', {
hooks: {
beforeRequest: [
(request) => {
request.headers.set('X-Requested-With', 'ky')
}
]
}
})
Hello,
You can use hooks for that:
https://github.com/sindresorhus/ky#hooks
this.$http.get('https://example.com', { hooks: { beforeRequest: [ (request) => { request.headers.set('X-Requested-With', 'ky') } ] } })
But doesn't this set the headers globally, that is, for the current and also future calls?
Normally this code will only apply the hooks to the current request
Ok. Thanks a lot!
El El jue, 5 nov 2020 a las 10:44, Gaetan SENN notifications@github.com escribió:
Normally this code will only apply the hooks to the current request
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nuxt/http/issues/128#issuecomment-722296958, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHNFSUO2EIBG3ZL5YZCOR3SOJ62XANCNFSM4QTL3HSQ .
What problem does this feature solve?
As specified in the Migration Guide, under Differences, both setHeaders and setToken are not scoped, which entails that we can only set headers globally, and not as per request. For small apps, this might not be a problem, but it is a problem for bigger ones that include authentication and different types of requests (get, post, put...), because in this cases, each request might need totally different headers.
What do the proposed changes look like?
Give the option to limit the existence of the current globally-defined headers to the next request, and then reset them. Or, enable the possibility to set scoped headers as part of the request options, as in the fetch method.