Post Reqeust JSON Data 10 – Gin Gonic Golang Web Framework

Post Reqeust JSON Data 10 – Gin Gonic Golang Web Framework

 

[sourcecode lang=”go” autolinks=”false” classname=”myclass” collapse=”false” firstline=”1″ gutter=”true” highlight=”1-3,6,9″ htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false” title=”Source Code main.go”]

// post request — Json Data post request
package main
import "github.com/gin-gonic/gin"
type Books struct {
Bookname string
Author string
Price string
}
// make every first letter capital in struct. otherwise errors 🙂
// create varible
// lets create an array/ slice
var books []Books
func main() {
// create new router
r := gin.Default()
// new post request … it has 2 args … path & function
r.POST("/", func(c *gin.Context) {
var book Books // now we can bind this variable and send back
c.BindJSON(&book)
// we need a struct/model.. Let create one
// sending response to user
// now append all the results in this array…
books = append(books, book)
c.JSON(200, books) // we are done // change book to books array/slice
// you send success message.. like c.String(200,"success…")
})
r.Run() // default port is 8080
}

[/sourcecode]

TAGS#

gin gonic tutorial
gin-gonic / examples
gin-gonic documentation
gin framework tutorial
gin web framework
gin router
golang gin context
gin router group

###Buy Me a Coffee https://www.buymeacoffee.com/khan1

Tony BB
 

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: