-
Axios 사용하기JavaScript/REST API 2024. 6. 12. 14:38
GET: 데이터 조회 axios.get(url)
POST: 데이터 등록 axios.post(url, data)
PUT: 데이터 수정 axios.put(url/id 값, data)
DELETE: 데이터 제거 axios.delete(url/id 값)
TEST API JS
import axios from "axios"; export default class Memo { constructor() { this.apiClient = axios.create({ baseURL: "http://localhost:3001", }); } async getMemos() { try { const response = await this.apiClient.get("memo"); const data = await response.data; return data; } catch (error) { console.log("memo error message", error); } } async createMemo(memo) { return await this.apiClient.post("memo", memo); } async deleteMemo(id) { return await this.apiClient.delete(`memo/${id}`); } async updateMemo(memo, id) { return await this.apiClient.put(`memo/${id}`, memo); } }
TEST API JSON
{ "memo": [ { "id": "5c151155-3217-4d12-8523-880a853698e8", "title": "제목1", "body": "내용1" }, { "id": "12d3629f-ecc6-4547-bc32-482220458d52", "title": "제목2", "body": "내용2" }, { "id": "767f607d-e528-4c7f-81cc-e47560179438", "title": "제목3", "body": "내용3" } ] }