Post Reqeust JSON Data 10 – Gin Gonic Golang Web Framework

Post Reqeust JSON Data 10 – Gin Gonic Golang Web Framework

 

 

Source Code

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

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: