Gin:路由配置和获取参数

VSCode 搭建Gin
Gin:路由配置和获取参数
Gin:路由抽离与分组
Gin:自定义控制器及控制器继承
Gin:中间件
Gin:文件上传
Gin:Cookie及二级域名共享Cookie
Gin:Session

1. 路由配置

package main

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

type UserInfo struct {
	Username string `form:"username" json:"username" binding:"required"`
	Password string `form:"password" json:"password" binding:"required"`
	Age      int    `form:"age" json:"age"`
}

type Department struct {
	Id       int    `json:"id"`
	DeptName string `json:"deptName"`
}

func main() {

	// 创建一个默认的路由引擎
	r := gin.Default()
	// 配置静态web目录;第一个参数表示路由,第二个参数表示映射的目录
	r.Static("/static", "./static")
	// 配置路由
	r.GET("/", func(c *gin.Context) {
		c.JSON(200, gin.H{
			// c.JSON:返回 JSON 格式的数据
			"message": "Hello world!",
		})
	})

	// 获取get请求传值
	r.GET("/login", func(c *gin.Context) {
		username := c.Query("username")
		// 如果没传,则使用默认值123
		password := c.DefaultQuery("password", "123")
		c.JSON(http.StatusOK, gin.H{
			// c.JSON:返回 JSON 格式的数据
			"name":     username,
			"password": password,
		})
	})

	// 获取get请求,path传值
	r.GET("/login/:username", func(c *gin.Context) {
		username := c.Param("username")
		c.JSON(http.StatusOK, gin.H{
			// c.JSON:返回 JSON 格式的数据
			"name": username,
		})
	})

	// 获取post请求传值
	r.POST("/login", func(c *gin.Context) {
		username := c.PostForm("username")
		password := c.PostForm("password")
		c.JSON(http.StatusOK, gin.H{
			// c.JSON:返回 JSON 格式的数据
			"name":     username,
			"password": password,
		})
	})

	// 获取post form请求,结构体传值
	r.POST("/loginForm", func(c *gin.Context) {
		user := &UserInfo{}
		err := c.ShouldBind(&user)
		if err == nil {
			fmt.Printf("%#v \n", user)
			c.JSON(http.StatusOK, user)
		} else {
			c.JSON(http.StatusBadRequest, gin.H{
				"err": err.Error(),
			})
		}
	})

	// 获取post json请求,结构体传值
	r.POST("/loginJson", func(c *gin.Context) {
		dept := &Department{}
		err := c.ShouldBindJSON(&dept)
		if err == nil {
			fmt.Printf("%#v \n", dept)
			c.JSON(http.StatusOK, dept)
		} else {
			c.JSON(http.StatusBadRequest, gin.H{
				"err": err.Error(),
			})
		}
	})

	// 启动 HTTP 服务,默认在 0.0.0.0:8080 启动服务
	r.Run()

}

2. 获取get请求传值

r.GET("/login", func(c *gin.Context) {
	username := c.Query("username")
	// 如果没传,则使用默认值123
	password := c.DefaultQuery("password", "123")
	c.JSON(http.StatusOK, gin.H{
		// c.JSON:返回 JSON 格式的数据
		"name":     username,
		"password": password,
	})
})
  • http://localhost:8080/login?username=fisher

在这里插入图片描述

3. 获取get请求,path传值

r.GET("/login/:username", func(c *gin.Context) {
	username := c.Param("username")
	c.JSON(http.StatusOK, gin.H{
		// c.JSON:返回 JSON 格式的数据
		"name": username,
	})
})
  • http://localhost:8080/login/fisher

在这里插入图片描述

4. 获取post请求传值

r.POST("/login", func(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	c.JSON(http.StatusOK, gin.H{
		// c.JSON:返回 JSON 格式的数据
		"name":     username,
		"password": password,
	})
})
  • http://localhost:8080/login

在这里插入图片描述

5. 获取post form请求,结构体传值

type UserInfo struct {
	Username string `form:"username" json:"username" binding:"required"`
	Password string `form:"password" json:"password" binding:"required"`
	Age      int    `form:"age" json:"age"`
}
r.POST("/loginForm", func(c *gin.Context) {
	user := &UserInfo{}
	err := c.ShouldBind(&user)
	if err == nil {
		fmt.Printf("%#v \n", user)
		c.JSON(http.StatusOK, user)
	} else {
		c.JSON(http.StatusBadRequest, gin.H{
			"err": err.Error(),
		})
	}
})
  • http://localhost:8080/loginForm

在这里插入图片描述

6. 获取post json请求,结构体传值

type Department struct {
	Id       int    `json:"id"`
	DeptName string `json:"deptName"`
}
r.POST("/loginJson", func(c *gin.Context) {
	dept := &Department{}
	err := c.ShouldBindJSON(&dept)
	if err == nil {
		fmt.Printf("%#v \n", dept)
		c.JSON(http.StatusOK, dept)
	} else {
		c.JSON(http.StatusBadRequest, gin.H{
			"err": err.Error(),
		})
	}
})
  • http://localhost:8080/loginJson

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值