LoginSignup
0
1

More than 5 years have passed since last update.

CookieのセットとCookieを指定してリクエスト

Last updated at Posted at 2018-09-07

Cookieについて考える

javascriptのfetchAPIでCookieのセットの仕方を調べてもいいのが出てこなかったので生のテキストでセットする方法を調べました.

サーバ側のCookieのセットの仕方

SetCookie: "任意の名前" = 値; Max-Age=<non-zero-digit>

リクエスト時のCookieのセットの仕方

Cookie: "任意の名前" = 値

fetchAPIでの実装は以下のようになります

fetch("http://******.com/*****", {
  method: 'GET',
  credentials: 'include',
  headers: new Headers({
    "Cookie": "hogehoge=fugafuga; Max-Age=1000 * 60 * 60 * 60 * 24 * 7",
  })
}).then(res => res.json())
.catch(error => console.error('Error:', error))
.then(response => this.setState({score: response.score}))

リクエスト時にもSetCookieを使っていたのでCookieはセットされてるのにサーバ側で取得できないという不思議な現象が起きました,,
ライブラリばかりに頼らずに,ネットワークの基礎(?)的な部分を理解してライブラリが何をしてくれているのかを理解するのも重要だと思いました!

0
1
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
1