すべてのCookie
fetch
のheaders
にCookie: cookieStore.toString()
を記述するとブラウザに設定されているすべてのCookieをセットできます。
'use server'
// ...
export async function sample() {
const cookieStore = await cookies()
const res = await fetch(
`http://example.com/sample`,
{
method: 'POST',
headers: {
// ...
Cookie: cookieStore.toString(),
},
// ...
},
)
// ...
}
特定のCookie
特定のCookieのみセットする場合には以下のようにします。
'use server'
// ...
export async function sample() {
const cookieStore = await cookies()
const sampleCookie = cookieStore.get('sample')
const res = await fetch(
`http://example.com/sample`,
{
method: 'POST',
headers: {
// ...
Cookie: `${sampleCookie?.name}=${sampleCookie?.value}`,
},
// ...
},
)
// ...
}