javascript - Nuxt 3 useFetch isn't including the Cookie header in the request - Stack Overflow

I'm using Nuxt 3 to make an external request to another server: I'm trying to pass a cookie v

I'm using Nuxt 3 to make an external request to another server: I'm trying to pass a cookie variable in the header, but examining the network request, it's not present. Here is my posable:

const {data, error} = await useFetch(";, {
    query: {
        origin: "example",
        qty: "1",
        sku: product.sku
    },
    onRequest({request, options}) {
        options.query = options.query || {}
        options.headers = options.headers || {}

        if (cart_session.value && cart_session.value.session) {
            options.headers["Cookie"] = `${config.public.cart_session.name}=${cart_session.value.session};`
        }

       console.log("[fetch request]", request, options)
    },
    onRequestError({request, options, error}) {
        // Log error
        console.log("[fetch request error]", request, error)
    },
    onResponse({request, response, options}) {
        console.log("[fetch response]", request, response._data, options)
        // cart_session.value = response._data
    },
    onResponseError({request, response, options}) {
        // Log error
        console.log("[fetch response error]", request, response.status, response.body)
    }
})

I have some login in place to check if the variable is set, which it is. I've also tried passing the Cookie parameter in the header array without any luck. Nuxt's documentation seems a bit limited so I'm not sure if it's even possible through their fetch posable. If not, I'm open to another solution.

I've tried passing the cookie header in multiple ways without any luck. I've also tried making a /server/api/ function without luck.

I'm using Nuxt 3 to make an external request to another server: I'm trying to pass a cookie variable in the header, but examining the network request, it's not present. Here is my posable:

const {data, error} = await useFetch("https://eo761aoiqhvo0xx.m.pipedream", {
    query: {
        origin: "example",
        qty: "1",
        sku: product.sku
    },
    onRequest({request, options}) {
        options.query = options.query || {}
        options.headers = options.headers || {}

        if (cart_session.value && cart_session.value.session) {
            options.headers["Cookie"] = `${config.public.cart_session.name}=${cart_session.value.session};`
        }

       console.log("[fetch request]", request, options)
    },
    onRequestError({request, options, error}) {
        // Log error
        console.log("[fetch request error]", request, error)
    },
    onResponse({request, response, options}) {
        console.log("[fetch response]", request, response._data, options)
        // cart_session.value = response._data
    },
    onResponseError({request, response, options}) {
        // Log error
        console.log("[fetch response error]", request, response.status, response.body)
    }
})

I have some login in place to check if the variable is set, which it is. I've also tried passing the Cookie parameter in the header array without any luck. Nuxt's documentation seems a bit limited so I'm not sure if it's even possible through their fetch posable. If not, I'm open to another solution.

I've tried passing the cookie header in multiple ways without any luck. I've also tried making a /server/api/ function without luck.

Share Improve this question asked Jun 22, 2023 at 18:01 Keith PetrilloKeith Petrillo 1754 silver badges12 bronze badges 3
  • 1 I'm not sure if it works with useFetch but I know it works with $fetch: you can add credentials: 'include' property to the request object. Please try and let me know, so I will write a proper answer. – learntheropes Commented Jun 22, 2023 at 19:32
  • By reading the useFetch documentation, I see that it is a wrapper around $fetch, so the previous suggest should definitely work. At least it solved the issue in one of my project. – learntheropes Commented Jun 23, 2023 at 11:22
  • I have the same problem - did you solve this, and how?

    发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744367718a4570790.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信