서버가 보낸 쿠키를 저장했다가 이후 요청에 자동으로 실어 보내는 레시피입니다. 세션 기반 로그인 흐름에 필수입니다.
서버가 보낸 쿠키를 저장했다가 이후 요청에 자동으로 실어 보내는 레시피입니다. 세션 기반 로그인 흐름에 필수입니다.
# save cookies from login, then reuse them:
curl -c cookies.txt -X POST https://example.com/login \
-d 'user=alice&pass=s3cret'
curl -b cookies.txt https://example.com/dashboard
# or send a cookie inline:
curl -b 'session=abc123' https://example.com/dashboard// browser: send cookies with the request (same-origin sends them by default)
await fetch('https://example.com/dashboard', {
credentials: 'include' // needed for cross-origin cookies
});
// Set-Cookie from the response is handled by the browser, not readable JSimport requests
s = requests.Session() # persists cookies across requests
s.post('https://example.com/login', data={'user': 'alice', 'pass': 's3cret'})
res = s.get('https://example.com/dashboard') # cookies sent automaticallycurl은 `-c <파일>`로 응답의 `Set-Cookie`를 쿠키 자(cookie jar)에 저장하고, `-b <파일>`로 그 쿠키를 다음 요청에 실어 보냅니다. `-b 'name=value'`처럼 인라인으로 직접 지정할 수도 있습니다. `requests`는 `Session` 객체가 자동으로 쿠키를 유지합니다.
브라우저 `fetch`에서 크로스 오리진 요청에 쿠키를 보내려면 `credentials: 'include'`를 지정해야 하고, 서버는 `Access-Control-Allow-Credentials: true`와 구체적인 `Access-Control-Allow-Origin`(와일드카드 `*` 불가)을 응답해야 합니다. `Set-Cookie`가 `HttpOnly`면 JS에서 `document.cookie`로 읽을 수 없습니다.
쿠키 속성에 주의하세요. `Secure`는 HTTPS에서만 전송, `SameSite=Strict|Lax|None`은 크로스 사이트 전송 여부를 제어하며 `SameSite=None`은 반드시 `Secure`와 함께 써야 합니다. 이 속성들이 CSRF 방어와 크로스 사이트 동작에 직접 영향을 줍니다.