getting started with get and post method in gin framework

  • GET and POST method to get the data submitted by the front end.
  • Blind the data passed from front end to the back-end structure

GET and POST method to get the data submitted by the front end.

#1.get method to get the data passed by hyperlink

//pass parameters by get method
types := c.Query("types")
//pass parameters by get method with default value
page := c.DefaultQuery("page", "1")

#2.get the data submitted through the post method 

//get the data submitted by client through post method by default
username := c.PostForm("username")
//when the pwd value is empty,give it a default value
pwd := c.DefaultPostForm("pwd", "123456")

#install gin

go get -u github.com/gin-gonic/gin

#create a main.go

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()

	//Configure template engine to load everything in the template
	r.LoadHTMLGlob("templates/**/*")
	//pass parameters through the get method
	//http://localhost:8088/news?types=2&page=4
	r.GET("/news", func(c *gin.Context) {
		//
		types := c.Query("types")
		//pass parameters by get method with default value
		page := c.DefaultQuery("page", "1")
		c.JSON(http.StatusOK, gin.H{
			"types": types,
			"page":  page,
		})
	})
	//get method to access the admin/index.html page
	r.GET("/", func(c *gin.Context) {
		c.HTML(http.StatusOK, "admin/index.html", gin.H{
			"title": "admin page",
		})
	})
	//submit data to the server through the post method
	r.POST("doreg", func(c *gin.Context) {
		//the post method gets the value of the username submitted by the front-end client
		username := c.PostForm("username")
		//when the pwd value is empty,give it a default value
		pwd := c.DefaultPostForm("pwd", "123456")
		c.JSON(http.StatusOK, gin.H{
			"usernmae": username,
			"pwd":      pwd,
		})
	})
	r.Run(":8088")
}

#create templates/admin/index.html

{{define "admin/index.html"}}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>admin page</title>
</head>
<body>
<h3>admin page</h3>
<form action="/doreg" method="POST">
  <p>user:<input type="text" name="username"/></p>
  <p>password:<input type="password" name="pwd"/></p>
  <p><input type="submit" value="register"/></p>
</form>
</body>

{{end}}

#Execute result

go run main.go

//Listening and serving HTTP on :8088
//[GIN] 2022/11/04 - 05:11:10 | 200 |    1.595868ms |       127.0.0.1 | GET      //"/news?types=2&page=4"
//[GIN] 2022/11/04 - 05:11:14 | 200 |     637.048µs |       127.0.0.1 | GET      //"/"
//[GIN] 2022/11/04 - 05:11:17 | 200 |      114.94µs |       127.0.0.1 | POST     //"/doreg"

http://localhost:8088/news?types=2&page=4


http://localhost:8088/

 Blind the data passed from front end to the back-end structure

  •  the value passed by get ,bind to the structure
  • the value passed by post ,bind to the structure

# the value passed by get ,bind to the structure

#1.First define the sturcture

// define structure
type Userinfo struct {
	Username string `form:"username" json:"username"`
	Password string `form:"password" json:"password"`
}

#2.implement the binding between the sctucture and the passing value of get

	//?username=john&password=1234556
	var userinfo Userinfo
	if err := c.ShouldBind(&userinfo); err == nil {
		c.JSON(http.StatusOK, userinfo)
	} else {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
	}

#3.create ./controllers/admin/userController.go

package admin

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type UserController struct{}

// define structure
type Userinfo struct {
	Username string `form:"username" json:"username"`
	Password string `form:"password" json:"password"`
}

func (con UserController) Index(c *gin.Context) {
	c.String(200, "routing pockets")

	//get the value passed by get ,bind to the structure
	//?username=john&password=1234556
	var userinfo Userinfo
	if err := c.ShouldBind(&userinfo); err == nil {
		c.JSON(http.StatusOK, userinfo)
	} else {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
	}
}

#5.create ./routers/adminRouters.go

package routers

import (
	"ginbind/controllers/admin"
	"github.com/gin-gonic/gin"
)

func AdminRouterInit(r *gin.Engine) {
	adminRouters := r.Group("/admin")
	{
		adminRouters.GET("/", admin.UserController{}.Index)

	}
}

#6.create ./main.go

package main

import (
	"ginbind/routers"
	"github.com/gin-gonic/gin"
)

func main() {
	//1.create a route with default engine
	r := gin.Default()
	//register routing packets in main
	routers.AdminRouterInit(r)

	r.Run(":8089")
}

#Execute result:

http://localhost:8089/admin/?username=john&password=1234556

#Listening and serving HTTP on :8089
#[GIN] 2022/11/05 - 06:03:12 | 200 |     213.946µs |       127.0.0.1 | GET      #"/admin/?username=john&password=1234556"

#routing pockets{"username":"john","password":"1234556"}

the value passed by post ,bind to the structure

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值