HTTP Challenge Game


Posted by 小小碼農 on 2021-05-08

Lidemy HTTP Challenge

大家都可以來挑戰一下!

這是一個串接 API 的娛樂挑戰,同時加深對於 HTTP 通訊協定的觀念,裡面有很多很多彩蛋,當然大部分是有一定年紀的才知道,這次使用 Node.js 來通關 🚀

LV.0

關卡要求
照著文件說明打上 {token},沒有難度

參數部分

  • token={...},當成功解決關卡就會得知 token 內容,代入即可前往下關
  • &hint=1,看提示用

解法
輸入網址,前往下一關
https://lidemy-http-challenge.herokuapp.com/lv1?token={GOGOGO}


LV.1

關卡要求
得到一份之後會用到的文件

使用 get 方法把自己的 name 傳給 Server

解法
操作網址列帶入參數即可
https://lidemy-http-challenge.herokuapp.com/lv1?token={GOGOGO}&name=oliver

得知 token 為 {HellOWOrld}


LV.2

關卡要求
操作網址列帶入參數 (id 54 ~ 58) 即可

解法
https://lidemy-http-challenge.herokuapp.com/lv2?token={HellOWOrld}&id=56

依據代入試試看,得知 id = 56,token = {5566NO1}


LV.3

查看 LV.1 時得到的API文件

關卡要求
使用 POST 新增書籍,書名是《大腦喜歡這樣學》,ISBN 為 9789863594475,新增完查看 body,再將 id 當作參數傳入網址列

解法

const request = require("request");

const URL = " https://lidemy-http-challenge.herokuapp.com/api";

request.post(
  {
    url: `${URL}/books`,
    form: {
      name: "《大腦喜歡這樣學》",
      ISBN: 9789863594475,
    },
  },
  (err, res, body) => {
    console.log("res:", res);
    console.log("body:", body);
  }
);

執行後獲得 id 為 1989 ,從網址列傳給 Server

https://lidemy-http-challenge.herokuapp.com/lv3?token={5566NO1}&id=1989

得知 token 為 {LEarnHOWtoLeArn}


之後皆須查看 API 文件,以下省略說明


LV.4

關卡要求
查詢書籍中含有「世界」二字的書,且作者為村上春樹,再將此書 id 當作參數傳給 Server

解法
直接帶入 q 無效,需要使用 encodeURI() 轉換網址

const request = require("request");
let str = "世界";

request.get(
  {
    uri: encodeURI(
      `https://lidemy-http-challenge.herokuapp.com/api/books?q=${str}`
    ),
  },
  (err, res, body) => {
    console.log(JSON.parse(body));
  }
);

得知 id = 79,傳給 Server
https://lidemy-http-challenge.herokuapp.com/lv4?token={LEarnHOWtoLeArn}&id=79

token 為 {HarukiMurakami}


LV.5

關卡要求
刪除一本 id 是 23 的書

解法
使用 delete 方法,得到系統回傳的 token

const request = require("request");

request(
  {
    method: "DELETE",
    uri: "https://lidemy-http-challenge.herokuapp.com/api/books/23",
  },
  (err, res, body) => {
    console.log("刪除成功");
    console.log(JSON.parse(body));
  }
);

得知 token 為 {CHICKENCUTLET}


LV.6

關卡要求
獲得一組帳號密碼:

  • 帳號 : admin
  • 密碼 : admin123
    登入後,呼叫 /me 的 endpoint,得到一組 email 並傳給 Server

解法
Node.js 可使用 Buffer.from() 進行 base64 編碼,準備好一組字串 base64(username:password)
再將其加入請求的 header 中

const request = require("request");

let account = "admin";
let pwd = "admin123";

let base64Str = Buffer.from(`${account}:${pwd}`).toString("base64");

const options = {
  uri: "https://lidemy-http-challenge.herokuapp.com/api/v2/me",
  headers: {
    Authorization: `Basic ${base64Str}`,
  },
};

const callback = (err, res, body) => {
  console.log(JSON.parse(body));
};

request.get(options, callback);

執行後得知 email 為 lib@lidemy.com ,使用 query string 傳給 Server
https://lidemy-http-challenge.herokuapp.com/lv6?token={CHICKENCUTLET}&q=lib@lidemy.com


LV.7

關卡要求
刪除 id 是 89 的書籍

解法
與上題差別不大,修改方法以及 API 即可

const request = require("request");

const URL = " https://lidemy-http-challenge.herokuapp.com/api/v2";

let account = "admin";
let pwd = "admin123";

let base64Str = Buffer.from(`${account}:${pwd}`).toString("base64");

const options = {
  uri: `${URL}/books/89`,
  headers: {
    Authorization: `Basic ${base64Str}`,
  },
};
const callback = (err, res, body) => {
  console.log("刪除成功");
  console.log(JSON.parse(body));
};

request.delete(options, callback);

// id = 89, delete

執行後得知 token 為 {HsifnAerok}


LV.8

關卡要求
找到書名含有「我」字,作者為四個字,ISBN 最後一碼為 7 的書,將他的 ISBN 最後一碼改為 3

解法

  • 查詢書籍,獲得正確 id 為 72
  • 修改書籍
const request = require("request");

let str = "我";
let account = "admin";
let pwd = "admin123";

let base64Str = Buffer.from(`${account}:${pwd}`).toString("base64");

const findOptions = {
  uri: encodeURI(
    `https://lidemy-http-challenge.herokuapp.com/api/v2/books?q=${str}`
  ),
  headers: {
    Authorization: `Basic ${base64Str}`,
  },
};

const patchOptions = {
  uri: "https://lidemy-http-challenge.herokuapp.com/api/v2/books/72",
  contentType: "application/x-www-form-urlencoded",
  headers: {
    Authorization: `Basic ${base64Str}`,
  },
  form: {
    name: "日日好日:茶道教我的幸福15味【電影書腰版】",
    ISBN: 9981835423,
  },
};

// id = 72, ISBN: 9981835427
const findOneBook = (err, res, body) => {
  const data = JSON.parse(body);
  for (let i of data) {
    if (i.author.length == 4 && i.ISBN[9] == 7) {
      return i;
    }
  }
};

const patchOneBook = (err, res, body) => {
  console.log(JSON.parse(body));
};

// 查詢書籍
request.get(findOptions, findOneBook);
// 修改書籍
request.patch(patchOptions, patchOneBook);

這部分應該還可以寫的更好,重用性更高,日後再回頭來看如何重構
執行後得知 token 為 {NeuN}


LV.9

關卡要求

  • 帶上一個 X-Library-Number 的 header,我們圖書館的編號是 20
  • 伺服器會用 user agent 檢查是否是從 IE6 送出的 Request,不是的話會擋掉

達成以上兩個要求,拿到系統資訊之後取得 version 欄位內的值,並傳回 Server

解法

  • 補上對應的 header : X-Library-Number
  • 偽造 IE6 的 User-Agent, 需查找關鍵字User-Agent用法
const request = require("request");

let account = "admin";
let pwd = "admin123";

let base64Str = Buffer.from(`${account}:${pwd}`).toString("base64");

const options = {
  url: encodeURI("https://lidemy-http-challenge.herokuapp.com/api/v2/sys_info"),
  headers: {
    Authorization: `Basic ${base64Str}`,
    "X-Library-Number": 20,
    "User-Agent": "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)",
  },
};

const callback = (err, res, body) => {
  console.log(JSON.parse(body));
};

request.get(options, callback);

得知 version 值為 1A4938Jl7 , 使用 query string 傳給 Server
https://lidemy-http-challenge.herokuapp.com/lv9?token={NeuN}&version=1A4938Jl7

得知 token 為 {duZDsG3tvoA}


LV.10

最後一題就普通邏輯遊戲,小時候都玩過,慢慢試

題目要求
出題者會出一個四位數不重複的數字,例如說 9487。
你如果猜 9876,我會跟你說 1A2B,1A 代表 9 位置對數字也對,2B 代表 8 跟 7 你猜對了但位置錯了。

開始吧,把你要猜的數字放在 query string 用 num 當作 key 傳給我

解法
最後得知正確數字為 9613

執行後得知 token 為 {IhateCORS}


挑戰心得

蠻有趣也設計的蠻好的,每一關都有東西要讓你了解,基礎加密、驗證、文件使用方法等,彩蛋也可以回頭再看,還有周杰倫的半島鐵盒(小爆雷),可以邊聽邊寫挑戰 🤣🤣


#HTTP #API







Related Posts

[C#] .net framework 使用 Autofac 註冊多組資料庫連線

[C#] .net framework 使用 Autofac 註冊多組資料庫連線

COSCUP 2020 貼紙蒐集者

COSCUP 2020 貼紙蒐集者

在 Ubuntu 上安裝 powerline

在 Ubuntu 上安裝 powerline


Comments