docs: fix issue in cookie passing example (#28223)

This commit is contained in:
Johan Krijt 2024-07-19 15:12:34 +02:00 committed by GitHub
parent d361991933
commit 228b9f6820
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -466,13 +466,13 @@ Be very careful before proxying headers to an external API and just include head
If you want to pass on/proxy cookies in the other direction, from an internal request back to the client, you will need to handle this yourself.
```ts [composables/fetch.ts]
import { appendResponseHeader, H3Event } from 'h3'
import { appendResponseHeader, H3Event, splitCookiesString } from 'h3'
export const fetchWithCookie = async (event: H3Event, url: string) => {
/* Get the response from the server endpoint */
const res = await $fetch.raw(url)
/* Get the cookies from the response */
const cookies = (res.headers.get('set-cookie') || '').split(',')
const cookies = splitCookiesString(res.headers.get('set-cookie') || '')
/* Attach each cookie to our incoming Request */
for (const cookie of cookies) {
appendResponseHeader(event, 'set-cookie', cookie)