Last active
March 28, 2022 14:27
-
-
Save ZRuei/61b289c47052427a9e29410ef927cee2 to your computer and use it in GitHub Desktop.
Lidemy HTTP Chellenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 練習用 axios | |
| // --- 第 8 關 --- /lv8?token={HsifnAerok} | |
| // 我昨天在整理書籍的時候發現有一本書的 ISBN 編號跟系統內的對不上,仔細看了一下發現我當時輸入系統時 key 錯了。 | |
| // 哎呀,人老了就是這樣,老是會看錯。 | |
| // 那本書的名字裡面有個「我」,作者的名字是四個字,key 錯的 ISBN 最後一碼為 7,只要把最後一碼改成 3 就行了。 | |
| // 對了!記得要用新的系統喔,舊的已經完全廢棄不用了。 | |
| const axios = require('axios'); | |
| const auth = `Basic ${Buffer.from('admin:admin123').toString('base64')}`; | |
| const bookstoreConfig = { | |
| baseURL: 'https://lidemy-http-challenge.herokuapp.com/api/v2', | |
| headers: { | |
| 'content-type': 'application/x-www-form-urlencoded', | |
| Authorization: auth | |
| }, | |
| }; | |
| const query = encodeURI('我'); | |
| const result = () => { | |
| axios.get(`/books?q=${query}`, bookstoreConfig) | |
| .then((res) => { | |
| const books = res.data; | |
| books.map((book) => { | |
| if (book.author.length === 4 && book.ISBN[book.ISBN.length - 1] === '7') console.log(book); | |
| }); | |
| }) | |
| .catch((err) => console.log(err)); | |
| }; | |
| result(); | |
| // --------------- | |
| // { | |
| // id: 72, | |
| // name: '日日好日:茶道教我的幸福15味【電影書腰版】', | |
| // author: '森下典子', | |
| // ISBN: '9981835427' | |
| // } | |
| // --------------- | |
| // 注意資料格式 | |
| axios.patch('/books/72', 'name=日日好日:茶道教我的幸福15味【電影書腰版】&ISBN=9981835423', bookstoreConfig) | |
| .then((res) => { | |
| console.log(res); | |
| }) | |
| .catch((err) => { | |
| console.log(err); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment