LoginSignup
1
2

【備忘録】Axios のHTTPメソッド一覧 (コピペ)

Posted at

備忘録(編集方法いまだよくわからず)

import axios from 'axios';

const BASE_URL = 'http://your-api-domain.com/ApiExample';

// GET: TODOリストの取得
const getTodos = async () => {
try {
const response = await axios.get(${BASE_URL}/todos);
return response.data;
} catch (error) {
console.error('Error getting todos:', error);
}
}

// POST: TODOの追加
const addTodo = async (name, todo) => {
try {
const response = await axios.post(${BASE_URL}/todos, {
name,
todo,
completed: false
});
return response.data;
} catch (error) {
console.error('Error adding todo:', error);
}
}

// PUT: TODOの更新
const updateTodo = async (id, updatedData) => {
try {
const response = await axios.put(${BASE_URL}/todos/${id}, updatedData);
return response.data;
} catch (error) {
console.error('Error updating todo:', error);
}
}

// DELETE: TODOの削除
const deleteTodo = async (id) => {
try {
const response = await axios.delete(${BASE_URL}/todos/${id});
return response.data;
} catch (error) {
console.error('Error deleting todo:', error);
}
}

// 使用例
(async () => {
// TODOの追加
const newTodo = await addTodo('John', 'Buy groceries');
console.log('Added todo:', newTodo);

// TODOリストの取得
const todos = await getTodos();
console.log('Todos:', todos);

// TODOの更新
if (todos.length > 0) {
    const updatedTodo = await updateTodo(todos[0].id, { completed: true });
    console.log('Updated todo:', updatedTodo);
}

// TODOの削除
if (todos.length > 0) {
    await deleteTodo(todos[0].id);
    console.log('Todo deleted.');
}

})();

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2