使用 Go 中的 Gin 框架创建一个具有 JSON API 所需大部分核心功能的 JSON API 的示例。
An example of using the Gin framework in Go to create a JSON api with most of the core functionality a JSON api needs.
Install Glide
curl https://glide.sh/get | sh
# or, on OSX
brew install glideCheck out the project and install it's dependencies
$ go get github.com/sbecker/gin-api-demo
$ cd $GOPATH/src/github.com/sbecker/gin-api-demo/
$ glide installIn one terminal, run the server:
$ cd $GOPATH/src/github.com/sbecker/gin-api-demo
$ go run main.goIn another terminal, try making requests:
# Get all users as a non-admin - see only one user, with a subset of json attributes
$ curl -H 'Authorization: Bearer userA' -i http://localhost:8080/users# Get all users as an admin - see all users, with all json attributes
$ curl -H 'Authorization: Bearer userB' -i http://localhost:8080/users# Try to get all users as a non-user - get 401 Unauthorized
$ curl -i http://localhost:8080/users# Get a single user as a non-admin - see a subset of json attributes
$ curl -H 'Authorization: Bearer userA' -i http://localhost:8080/users/1# Get a single user as an admin - see a subset of json attributes
$ curl -H 'Authorization: Bearer userB' -i http://localhost:8080/users/1# Try to get a different user than self - get 404 Not Found
$ curl -H 'Authorization: Bearer userA' -i http://localhost:8080/users/2# Try to get a single user as a non-user, get 401 Unauthorized
$ curl -i http://localhost:8080/users/2Request-Id headers: Each API request has an associated request identifier暂无开放 Issues,或尚未同步最近议题。