Strapi is an open-source headless CMS (Content Management System) built to work with any frontend and any database. It provides developers with a flexible and customizable solution for building content-rich applications, giving them full control over the API, data model, and platform.
Why Strapi? ποΈ
Strapi sets itself apart from other CMS solutions in several ways:
Open-source and Self-hosted: Unlike most CMS platforms, Strapi allows you to self-host your instance, offering greater flexibility, control, and security.
Headless by Design: Strapi is built as a headless CMS, meaning the frontend and backend are decoupled, giving you the freedom to use any frontend technologyβReact, Vue, Angular, or even native mobile apps.
Customizable APIs: Strapi lets you customize the API for your specific needs, whether itβs REST or GraphQL, making it ideal for projects requiring complex data structures.
Database Agnostic: You can use Strapi with multiple databases like PostgreSQL, MongoDB, MySQL, or SQLite, offering seamless flexibility for different use cases.
This guide provides an overview of basic operations for managing products and images using Strapi, with PostgreSQL as the database. Ensure you have PostgreSQL and PgAdmin 4 installed if you're using PostgreSQL for your Strapi setup.
Prerequisites π οΈ
- Install and set up Strapi following the official Strapi Quick Start guide.
- Ensure you have PostgreSQL installed in your system.
REST API Overview π³
For complete details, refer to the Strapi REST API documentation.
Products Collection API
Assume a Products
collection with fields:
-
Product_name
: String (Name of the product) -
img
: Media (Image ID associated with the product) -
state
: Boolean (Indicating product status)
1. Get All Products
Retrieve all products with their associated data (including images).
Endpoint:
GET http://localhost:1337/api/products?populate=*
-
populate=*
ensures all relational data, including images, are properly fetched.
2. Upload an Image
Upload an image to the Strapi server.
Endpoint:
POST http://localhost:1337/api/upload/
Request Body: (form-data)
-
key = files
(value is the image file uploaded from your browser).
3. Get All Uploaded Images
Retrieve all uploaded image files.
Endpoint:
GET http://localhost:1337/api/upload/files
4. Add a New Product
Create a new product entry in the Products
collection.
Endpoint:
POST http://localhost:1337/api/products
Request Body:
{
"data": {
"Product_name": "Samsung S4 old colored",
"state": true,
"img": 3
}
}
- The
"img": 3
field refers to the ID of the uploaded image, in this case, image ID3
.
5. Update a Product
Update a specific product using its documentId
.
Endpoint:
PUT http://localhost:1337/api/products/:documentId
Request Body:
{
"data": {
"Product_name": "Samsung S4 old updated",
"state": true,
"img": 3
}
}
Here, you update the product name while keeping the image ID (3
), which represents the already uploaded image.
π This document should serve as a quick reference for managing products and images in Strapi with PostgreSQL. For further customization or detailed configurations, consult the official Strapi documentation.
Top comments (0)