Skip to content

erikwehrmann/go-gin-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

go-gin-api

This project is a simple API I created to learn how to use the gin api routing and basic JSON reponses for a go API. It is not connected to database and the data is seeded by a local variable and store in said variable, meaning when you restart the service it will return to its default state. This is also ran on localhost using port 8000.

Running the Service

Start the application using:

go run .

Making a request to GET albums

curl http://localhost:8000/albums
[
  {
      "id": "1",
      "title": "Appetite for Destruction",
      "artist": "Guns N' Roses",
      "price": 36.99
  },
  {
      "id": "2",
      "title": "Ten",
      "artist": "Pearl Jam",
      "price": 29.99
  },
  {
      "id": "3",
      "title": "London Calling",
      "artist": "The Clash",
      "price": 16.99
  }
]

Making a request to POST albums

curl http://localhost:8000/albums \
    --include \
    --header "Content-Type: application/json" \
    --request "POST" \
    --data '{"id": "4","title": "2112","artist": "Rush","price": 49.99}'
{
    "id": "4",
    "title": "2112",
    "artist": "Rush",
    "price": 49.99
}

Check to see if the new albums has been added to the albums slice

curl http://localhost:8000/albums
[
  {
      "id": "1",
      "title": "Appetite for Destruction",
      "artist": "Guns N' Roses",
      "price": 36.99
  },
  {
      "id": "2",
      "title": "Ten",
      "artist": "Pearl Jam",
      "price": 29.99
  },
  {
      "id": "3",
      "title": "London Calling",
      "artist": "The Clash",
      "price": 16.99
  },
  {
    "id": "4",
    "title": "2112",
    "artist": "Rush",
    "price": 49.99
  }
]

Making a request to GET album

curl http://localhost:8000/albums/1
{
  "id": "2",
  "title": "Ten",
  "artist": "Pearl Jam",
  "price": 29.99
}

Will return error message if none is found

curl http://localhost:8000/albums/999
{
  "message": "album not found"
}

About

Learning how to create Golang API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages