On the previous post we learned how to return JSON responses in Go, now we will see how to build our first Rest API.
Let’s modify our previous example to have a route to handle the member resource:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
)
var members = []Member{Member{"someuser", "someuser@somedomain.com"}}
type Member struct {
Login string
Email string
}
func main() {
http.HandleFunc("/members", membersHandler)
http.ListenAndServe(":8080", nil)
}
func membersHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == "GET" {
j, _ := json.Marshal(members)
w.Write(j)
}
if r.Method == "POST" {
var m Member
b, _ := ioutil.ReadAll(r.Body)
json.Unmarshal(b, &m)
members = append(members, m)
j, _ := json.Marshal(m)
w.Write(j)
}
}
On line 17 we changed the first parameter from "/" to "/members", now our membersHandler will only handle requests to
the /members endpoint.
The membersHandler function:
- Sets the
"Content-Type"header toapplication/json. - Checks if the request method is
GET, if so it just returns all the members. - If the method is
POSTit saves the new member to the members array and then returns the newly created member.
When we issue a GET /members then we will see the following response:
$ curl -i localhost:8080/members
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 56
[{"Login":"someuser","Email":"someuser@somedomain.com"}]%We can also create a new member:
$ curl -d '{"Login":"anotheruser", "Email": "another@somedomain.com"}' -i localhost:8080/members
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 56
{"Login":"anotheruser","Email":"another@somedomain.com"}%The following posts I will show you how to have smarter routes and how to persist your members using MongoDB.