IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> Gin:路由配置和获取参数 -> 正文阅读

[网络协议]Gin:路由配置和获取参数

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

在这里插入图片描述

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-02-06 14:07:32  更:2022-02-06 14:08:39 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年10日历 -2024/10/5 11:19:39-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码