@@ -29,16 +29,34 @@ func getUser(c *gin.Context) {
29
29
c .JSON (http .StatusOK , gin.H {"message" : "Hello, " + name + "!" })
30
30
}
31
31
32
+ var users = []userDetail {
33
+ {ID : "1" , Name : "Blue Train" , Age : 56 },
34
+ {ID : "2" , Name : "Akash" , Age : 11 },
35
+ {ID : "3" , Name : "pradip" , Age : 56 },
36
+ }
37
+
32
38
func getUsers (c * gin.Context ) {
33
- var users = []userDetail {
34
- {ID : "1" , Name : "Blue Train" , Age : 56 },
35
- {ID : "2" , Name : "Akash" , Age : 11 },
36
- {ID : "3" , Name : "pradip" , Age : 56 },
37
- }
38
39
39
40
c .JSON (http .StatusOK , users )
40
41
}
41
42
43
+ // postAlbums adds an album from JSON received in the request body.
44
+ func addUser (c * gin.Context ) {
45
+
46
+ var newUser userDetail
47
+
48
+ // Call BindJSON to bind the received JSON to
49
+ // newAlbum.
50
+ if err := c .BindJSON (& newUser ); err != nil {
51
+ return
52
+ }
53
+
54
+ // Add the new album to the slice.
55
+ users = append (users , newUser )
56
+ c .IndentedJSON (http .StatusCreated , newUser )
57
+
58
+ }
59
+
42
60
func main () {
43
61
router := gin .Default ()
44
62
@@ -55,6 +73,7 @@ func main() {
55
73
// with param
56
74
router .GET ("/user/:name" , getUser )
57
75
router .GET ("/user" , getUsers )
76
+ router .POST ("/user" , addUser )
58
77
59
78
router .Run (":3000" )
60
79
}
0 commit comments