2022年10月28日 星期五

Deno 如何保留 cookie

自己試著用 Deno 寫小機器人

不過不管試幾次,登入後去其他 request 都做不到


我的 code 大致如下

const account = "foo@example.com"
const password = "bar123456789"
const host = "http://localhost:8080"

let cookie = await fetch(`${host}/login`, {
	body: `account=${account}&password=${password}`,
	method: "POST",
	headers: {"Content-type": "application/x-www-form-urlencoded"}
})
	.then(function(resp){
		return resp.headers.get("Set-cookies") || "" 	// 不加上 || "" 的話,cookie 就會被視作 nullable 變數,在 deno 可能會說 warning 或 error
	})

fetch(`${host}/some-request`, {
	headers: {"Cookie": cookie}
})
	.then(function(resp){
		return resp.text()
	}).then(function(data){
		console.log(data)
	})

以上這段 code 最終會 redirect 到首頁而已

因為 cookie 取不到(換言之, cookie 變數一直都會是空字串)


目前我是沒找到 Deno 會這樣的原因,但解決辦法倒是找到了。


會發生這種情況,都是在進行了 redirect 的情況

例如說你登入的 request 是 http://localhost:8080/login ,登入後會進入 http://localhost:8080/my-profile

不管是 Deno 還是瀏覽器,執行了 fetch(`http://localhost:8080/login`)都會自動進行重定向,所以最終 return 的 response 會是 http://localhost:8080/my-profile

而重定向後,無論是 Deno 還是瀏覽器, fetch 得到的 response 都不會保留 cookie。

對瀏覽器而言,可以靠 credentials: "include" 這設定解決無法讀取 cookie 的問題,但 Deno 畢竟沒有 origin 的概念,就沒法這樣處理了。

對 Deno 而言,要解決的辦法很單純,就是不讓其自動重定向

fetch 有個參數 redirect: "manual" 可以做到,而 code 就會如下:

// 近乎一樣的 fetch code,只是多了redirect: "manual"
let cookie = await fetch(`${host}/login`, {
	body: `account=${account}&password=${password}`,
	method: "POST",
	redirect: "manual"
	headers: {"Content-type": "application/x-www-form-urlencoded"}
})
	.then(function(resp){
		return resp.headers.get("Set-cookies") || "" 
	})

// 自行手動重定向,有些網站會在重定向時處理一些參數,還是 send request 到登入後的頁面比較保險
await fetch(`${host}/my-profile`, {
	headers: {Cookie: cookie}
})

// 前往自己想要去的頁面
fetch(`${host}/some-request`, {
	headers: {"Cookie": cookie}
})
	.then(function(resp){
		return resp.text()
	}).then(function(data){
		console.log(data)
	})

沒有留言:

張貼留言