GO-vatar series: API Learning & Development
DO NOT stay in your comfort zone forever. Keep learning and develop yourself.
Hi to whoever is reading this article, I hope we develop ourselves together.
This time I back with another recipe other than last recipe I shared to you. This recipe (I hope) is very simple. You can do it alone, because I’ve done it by myself.
Preparations
What should we do?
Setup GO
Please read chapter Preparation
in Recipe To Boil Web Automation With Go Language.
Setup Framework
$ go get -u github.com/gorilla/mux
Instructions
$ cd $GOPATH/project/go/src/github.com
$ mkdir golang-API && cd golang-API
Modify
main.go
Create and modify
not_found.go
inside endpoint folder.
Let’s run!
Remember the port we initialize in https://gist.github.com/mpermperpisang/51ce89c2c51d86d76c2f8bc3274d7249#file-main-go-L15.
$ go run main.go
Well, that’s the basic. Now let’s go to the next level.
For easy step I recommend you to clone this repository.
We need a little by little modification in main.go
.
GET Endpoint
r.HandleFunc(“/get”, example.Get).Methods(http.MethodGet)
- The endpoint is
localhost:8181/get
- Call function
Get
in packageexample
- Hit endpoint by method
GET
- Need header
Content-Type
with contentapplication/json
- Response status should be
200
- Response body should be like this
POST Endpoint
r.HandleFunc(“/post”, example.Post).Methods(http.MethodPost)
- The endpoint is
localhost:8181/post
- Call function
Post
in packageexample
- Hit endpoint by method
POST
- Need header
Content-Type
with contentapplication/json
- Response status should be
201
- Response body should be like this
PUT Endpoint
r.HandleFunc(“/put”, example.Put).Methods(http.MethodPut)
- The endpoint is
localhost:8181/put
- Call function
Put
in packageexample
- Hit endpoint by method
PUT
- Need header
Content-Type
with contentapplication/json
- Response status should be
202
- Response body should be like this
PATCH Endpoint
r.HandleFunc(“/patch”, example.Patch).Methods(http.MethodPatch)
- The endpoint is
localhost:8181/patch
- Call function
Patch
in packageexample
- Hit endpoint by method
PATCH
- Need header
Content-Type
with contentapplication/json
- Response status should be
200
- Response body should be like this
DELETE Endpoint
r.HandleFunc(“/delete”, example.Delete).Methods(http.MethodDelete)
- The endpoint is
localhost:8181/delete
- Call function
Delete
in packageexample
- Hit endpoint by method
DELETE
- Need header
Content-Type
with contentapplication/json
- Response status should be
200
- Response body should be like this
Easy right? No need many data content modification.
The endpoint URL above is very simple to begin with. Next we’ll try to add some data as a parameter or part of the URL.
I’m sure you see often endpoint like this :
localhost:8181/get/1
orlocalhost:8181/get?id=1
Mandatory URL
r.HandleFunc(“/example/biodata/{id}”, example.GetOneID).Methods(http.MethodGet)
- The endpoint is
localhost:8181/get/1
- Call function
GetOneID
in packageexample
- Hit endpoint by method
GET
Optional Parameter URL
r.Path(“/example/biodata”).Queries(“id”, “{id}”).HandlerFunc(example.GetParamOneID).Methods(http.MethodGet)
- The endpoint is
localhost:8181/get?id=1
- Call function
GetParamOneID
in packageexample
- Hit endpoint by method
GET
Both of the endpoint above need to read the ID.
Left picture the ID is integer but in the right side the ID is string. That’s just for example so you guys can see the different.
Well, that’s all. Easy recipe to create a simple API. Please enjoy! — MperMperPisang