0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

fetchでHttpOnly属性のCookieを渡す方法

Posted at

概要

HttpOnly属性のCookieを渡すコードについて、探しても意外と見つからなかったのでメモを残します。

コード

下記のようにheaderに明示的にCookieを渡すようにして、 credentials: 'include'を指定することでAPI側にHttpOnly属性のCookieを渡せます。

.ts
    const response = await fetch(${apiUrl}/api/get/me, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
         Cookie: cookieHeader, 
      },
      credentials: 'include', 
    });

余談

調べると credentials: 'include'だけ設定すれば自動でHttpOnly属性のCookieを渡せるなどのサイトもありましたが、私の環境ではできませんでした。
下記の環境で動作確認しています。
https://github.com/dbd-fish/react_sample

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?