Skip to content

Latest commit

 

History

History
116 lines (92 loc) · 1.91 KB

File metadata and controls

116 lines (92 loc) · 1.91 KB

API Reference — goTutorial

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)

Endpoints

  • Create Post
    • Method: POST
    • URL: /post
    • Body (JSON):
{
  "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):
{
  "posts": [ { /* post objects */ } ]
}
  • Show Post
    • Method: GET
    • URL: /post/:id
    • Example: /post/1
    • Success (200):
{ "post": { /* post object */ } }
  • Update Post
    • Method: PUT
    • URL: /posts/:id
    • Body (JSON):
{ "Title": "New title", "Body": "Updated body" }
  • Success (200): returns the updated post object.

  • Delete Post

    • Method: DELETE
    • URL: /post/:id
    • Example: /post/1
    • Success (200):
{ "message": "post deleted successfully for id:1" }

Example curl commands

Create:

curl -X POST http://localhost:8080/post \
  -H "Content-Type: application/json" \
  -d '{"Title":"Hello","Body":"Hello world"}'

List:

curl http://localhost:8080/posts

Show:

curl http://localhost:8080/post/1

Update:

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

Notes

  • The handlers use c.Bind() which will accept JSON or form-encoded bodies; prefer application/json.
  • Responses return 400 on DB errors with a JSON error field.