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 變數一直都會是空字串)

2022年10月18日 星期二

2022年10月17日 星期一

Less import 外部樣式

這是我在使用 Less 時遇到的問題

當我要引用外部的樣式,例如 google font:

@import url('https://fonts.googleapis.com/css2?family=Fredericka+the+Great');

@default-color: #333;

body{
	color: @default-color;
	font-family: 'Fredericka the Great', cursive;
}

會被編譯成如下的 css:

@font-face {
	font-family: 'Fredericka the Great';
	font-style: normal;
	font-weight: 400;
	src: url(https://fonts.gstatic.com/s/frederickathegreat/v15/9Bt33CxNwt7aOctW2xjbCstzwVKsIBVV-9Sk.ttf) format('truetype');
}
body {
	color: #333;
	font-family: 'Fredericka the Great', cursive;
}

可是,外部的 CSS 我並不想要直接將之轉換,我想要匯出的 CSS 依舊使用 @import

2022年10月6日 星期四

2022年10月5日 星期三

Java 用 Stream 將 List<obj> 變成 Map<key, List<obj>>

例如我手邊有 class Student


class Student{
	private int studentID, teacherID;
	private String studentName;
	public Student (int studentID, int teacherID, String studentName){
		this.studentID = studentID;
		this.teacherID = teacherID;
		this.studentName = studentName;
	}
	
	public int getStudentID(){return studentID;}
	public int getTeacherID(){return teacherID;}
	public String getStudentName(){return studentName;}
	
	public String toString(){
		return String.format("[Student %02d: %s]", studentID, studentName);
	}
	
}

假設我手邊有學生清單( List<Student>)

但我想看每個老師底下有哪些學生,換言之,想將之轉為 Map< int (teacherID), List<Student>>