sindresorhus / ky-universal

Use Ky in both Node.js and browsers
https://github.com/sindresorhus/ky
MIT License
670 stars 20 forks source link

response.headers.get('') bug? #13

Closed coldestheart closed 4 years ago

coldestheart commented 4 years ago

Hello, ive tried to migrate from axios (using nuxt), and start using '@nuxt/http' but when i was tryed to get header from the post response

import http from 'ky-universal'
const actions = {
  // Get Token
  async login(context, payload) {
    let data = null
    try {
      data = await http.post(
        process.env.apiUrl + '/api/content/create?type=User',
        {
          body: payload
        }
      )
    } catch (err) {}
    console.log(data.headers.get('Authorization'))
  },
}

console.log(data.headers.get('Authorization')) shows null, there is anyway to make it work?

in firefox network tab response header looks like:

HTTP/1.1 200 OK
Access-Control-Allow-Headers: Accept, Authorization, Content-Type
Access-Control-Allow-Origin: *
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2Nlc3MiOiJjb2xkQGNvbGQuY29sZCIsImF1ZCI6bnVsbCwiZXhwIjoxNTg0NTQyMzMwLCJpYXQiOm51bGwsImlzcyI6bnVsbCwianRpIjpudWxsLCJuYmYiOm51bGwsInN1YiI6bnVsbH0.SYAwu0h5xA6xNQQGqja9uItHq0Wbq-52I4CrT78N7U0
Cache-Control: max-age=2592000, public
Etag: MTU4MzkzNjUzMw==
Date: Wed, 11 Mar 2020 14:38:50 GMT
Content-Length: 0
sholladay commented 4 years ago

I don't know why it's not working, but Authorization is a request header, not a response header. This usage is invalid as far as the HTTP spec is concerned.

https://tools.ietf.org/html/rfc7235#section-4.2

coldestheart commented 4 years ago

Thx for an answer, sorry about this stupid question cause im newbie, i've just tried to use https://github.com/bosssauce/access/ and there was cfg.ResponseWriter.Header().Add("Authorization", "Bearer "+token), i thought that was right way to send token in header of response, sorry and thx again.