1. Các phương thức cơ bản
Methods | URLs | Actions |
GET | /products | findAll() |
GET | /products?categoryID=123&page=1 | |
GET | /products/:id | findByID() findOne() |
POST | /products/add | addOne() |
PATCH | /products/update/:id | updateOne() |
PATCH | /products/remove/:id | removeOne() |
PATCH | /products/restore/:id | restoreOne() |
PATCH | /products/removeList | removeList() |
PATCH | /products/restoreList | restoreList() |
PUT |
/products/replace
|
replaceOne()
|
DELETE | /products/destroy/:id | destroyOne() |
DELETE | /products/destroyList | destroyList() |
2. Demo Fetch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Resful API</title> </head> <body> <div> <button onclick="getAPI()">Get Users</button> <button onclick="postAPI()">Post Login</button> <button onclick="putAPI()">Put User</button> <button onclick="patchAPI()">Patch User</button> <button onclick="deleteAPI()">Delete User</button> <br> <button onclick="postAPI2()">Post Login 2</button> </div> <script> const getAPI = async () => { try { let url = 'https://reqres.in/api/users?page=2'; let response = await fetch(url); console.log(response); let responseJSON = await response.json(); console.log(responseJSON); } catch (error) { console.log('Failed to fetch data !!!', error.message); } } const postAPI = async () => { try { let config = { method: 'POST', headers: { 'Content-Type': 'application/json', }, } let url = 'https://reqres.in/api/login'; let response = await fetch(url, config); console.log(response); let responseJSON = await response.json(); console.log(responseJSON); } catch (error) { console.log('Failed to fetch data !!!', error.message); } } const postAPI2 = async () => { try { let url = "https://learn-api.jmaster.io:8443/api/login"; let config = { method: 'POST', headers: new Headers({ 'Content-Type': "application/x-www-form-urlencoded", 'Cookie': "JSESSIONID=B41705BC2C08E0B2A4CF1338348333CE", }), body: new URLSearchParams({ 'username': '0123456789', 'password': '123456', }), redirect: 'follow' }; let response = await fetch(url, config); console.log(response); let responseJSON = await response.json(); console.log(responseJSON); } catch (error) { console.log('Failed to fetch data !!!', error.message); } } const putAPI = async () => { try { let config = { method: 'PUT', body: JSON.stringify({ name: "James", job: "Student", }), headers: { 'Content-Type': 'application/json', }, } let url = 'https://reqres.in/api/users/2'; let response = await fetch(url, config); console.log(response); let responseJSON = await response.json(); console.log(responseJSON); } catch (error) { console.log('Failed to fetch data !!!', error.message); } } const patchAPI = async () => { try { let config = { method: 'PUT', body: JSON.stringify({ name: "Robert", job: "Farming", }), headers: { 'Content-Type': 'application/json', }, } let url = 'https://reqres.in/api/users/2'; let response = await fetch(url, config); console.log(response); let responseJSON = await response.json(); console.log(responseJSON); } catch (error) { console.log('Failed to fetch data !!!', error.message); } } const deleteAPI = async () => { try { let config = { method: 'DELETE', } let url = 'https://reqres.in/api/users/2'; let response = await fetch(url, config); let responseJSON = await response.json(); console.log(response); console.log(responseJSON); } catch (error) { console.log('Failed to fetch data !!!', error.message); } } </script> </body> </html> |