- Core API's
- >
- Todos API
- >
- Todos - Create
Todos - Create API
Create a new Todo.
Method POST
Body
{
"title": "Sample Todo Item",
"completed": true,
"user_id": 1,
"description": "This is a sample Todo item."
}
How to Use
fetch('https://simplecrudapi.com/api/todos', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
title: 'Sample Todo',
description: 'This is a sample todo item',
user_id: '',
completed: ''
})
})
.then(response => response.json())
.then(json => console.log(json))
.catch(error => console.error('Error posting data:', error));
Response
{
"data": {
"id": 1,
"title": "Sample Todo",
"description": "This is a sample todo item",
"completed": true,
"user": {
"id": 1,
"name": "Jhon Doe",
"email": "jhondoe@example.com",
"email_verified_at": "2019-08-24T14:15:22Z",
"created_at": "2019-08-24T14:15:22Z",
"updated_at": "2019-08-24T14:15:22Z"
},
"created_at": "2019-08-24T14:15:22Z",
"updated_at": "2019-08-24T14:15:22Z"
}
}
Error Responses
{
"message": "The given data was invalid.",
"errors": {
"user_id": [
"The user id must be an integer.",
"The selected user id is invalid."
],
"title": [
"The title field is required.",
"The title must be a string.",
"The title must be at least 1 character."
],
"description": [
"The description must be a string."
],
"completed": [
"The completed field is required.",
"The completed field must be true or false."
]
}
}
Try Now
fetch('https://simplecrudapi.com/api/todos', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
title: 'Sample Todo',
description: 'This is a sample todo item',
user_id: '',
completed: ''
})
})
.then(response => response.json())
.then(json => console.log(json))
.catch(error => console.error('Error posting data:', error));