Base URL (local): http://localhost:8080
The project exposes simple CRUD endpoints for a Post model. The Post model fields (JSON names) are:
ID(number, added by GORM)CreatedAt,UpdatedAt,DeletedAt(timestamps handled by GORM)Title(string)Body(string)
- Create Post
- Method:
POST - URL:
/post - Body (JSON):
- Method:
{
"Title": "My title",
"Body": "The post body"
}- Success response (200):
{
"post": {
"ID": 1,
"CreatedAt": "...",
"UpdatedAt": "...",
"DeletedAt": null,
"Title": "My title",
"Body": "The post body"
}
}- List Posts
- Method:
GET - URL:
/posts - Success (200):
- Method:
{
"posts": [ { /* post objects */ } ]
}- Show Post
- Method:
GET - URL:
/post/:id - Example:
/post/1 - Success (200):
- Method:
{ "post": { /* post object */ } }- Update Post
- Method:
PUT - URL:
/posts/:id - Body (JSON):
- Method:
{ "Title": "New title", "Body": "Updated body" }-
Success (200): returns the updated
postobject. -
Delete Post
- Method:
DELETE - URL:
/post/:id - Example:
/post/1 - Success (200):
- Method:
{ "message": "post deleted successfully for id:1" }Create:
curl -X POST http://localhost:8080/post \
-H "Content-Type: application/json" \
-d '{"Title":"Hello","Body":"Hello world"}'List:
curl http://localhost:8080/postsShow:
curl http://localhost:8080/post/1Update:
curl -X PUT http://localhost:8080/posts/1 \
-H "Content-Type: application/json" \
-d '{"Title":"Updated","Body":"Updated body"}'Delete:
curl -X DELETE http://localhost:8080/post/1- The handlers use
c.Bind()which will accept JSON or form-encoded bodies; preferapplication/json. - Responses return
400on DB errors with a JSONerrorfield.