This app is all about a CRUD API using Laravel.
The core functionality of the API:
- Display a list of posts.
- Delete a specific post.
- Add a brand new post.
- Update an existing post.
For the required software installation, you just have to open your terminal on the current folder and type in composer install
-
Create a brand new database with the name:
restapilaravel
-
Then in terminal run:
php artisan migrate:fresh
Start the Server
php artisan serve
- Base URL: At present this app can only be run locally and is not hosted as a base URL. The backend app is hosted at the default,
http://127.0.0.1:8000/
; - Authentication: This version of the application does not require authentication or API keys.
Errors are returned as JSON objects in the following format:
{
"error": 404,
"message": "Resource not found"
}
The API will return three error types when requests fail:
- 404: Resource Not Found
- 400: Bad request
- 405: Method not allowed
- Fetches a list of posts from the database.
- Request Arguments: None
- Returns: A JSON object with a single key, data, that contains an array of posts.
curl http://127.0.0.1:8000/posts
{
"data": [
{
"id": 1,
"title": "Brand new title",
"content": "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.",
"created_at": "2022-10-26T15:31:19.000000Z",
"updated_at": "2022-10-26T15:31:19.000000Z"
}
]
}
- Get a specific post based on his id
- Return: A JSON object with a single key, data, that contains a specific post.
- Request Arguments: None
curl http://127.0.0.1:8000/posts/1
{
"data": {
"id": 1,
"title": "Brand new title",
"content": "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.",
"created_at": "2022-10-26T15:31:19.000000Z",
"updated_at": "2022-10-26T15:31:19.000000Z"
}
}
- Create a brand new post
- Request Arguments: Required arguments are: 'title' and 'content'.
- Returns a success message and the id of the new post
curl http://127.0.0.1:8000/posts -X POST -H "Content-Type: application/json" -d '{"title":"A new title", "content":"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English."}'
{
"message": "Resource created successfully.",
"id": 2
}
- Update an existing post
- Request Arguments: Required arguments are: 'title' and 'content'.
- Returns a success message and the id of the updated post
curl http://127.0.0.1:8000/posts/1 -X PATCH -H "Content-Type: application/json" -d '{"title":"A new title", "content":"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English."}'
{
"message": "Resource updated successfully.",
"id": 2
}
- Deletes a specific post based on his id
- Request arguments: None
- Returns a success message and the id of the deleted question
curl -X DELETE http://127.0.0.1:8000/posts/2
{
"message": "Resource deleted successfully",
"id": 2
}