nuxt / http

Universal HTTP Module for Nuxt.js
https://http.nuxtjs.org
MIT License
229 stars 51 forks source link

Per-request (or scoped) headers #128

Closed kinxos closed 4 years ago

kinxos commented 4 years ago

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.

Captura de pantalla 2020-09-02 a las 18 10 12
gaetansenn commented 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')
      }
    ]
  }
})
kinxos commented 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')

      }

    ]

  }

})

But doesn't this set the headers globally, that is, for the current and also future calls?

gaetansenn commented 4 years ago

Normally this code will only apply the hooks to the current request

kinxos commented 4 years ago

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 .