Gin_GetPostParam.go 916 Bytes
Newer Older
Ford committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
package main

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

func main() {
	//获取web引擎对象
	engine := gin.Default()

	//Get请求的处理
	engine.GET("/hello", func(context *gin.Context) {
		fmt.Println(context.FullPath())

		username := context.Query("name")
		fmt.Println(username)

		context.Writer.Write([]byte("Hello," + username))
	})

	// Post请求
	/*
		POST请求以表单的形式提交数据,除了可以使用context.PostForm获取表单数据以外,还可以使用context.GetPostForm来获取表单数据。
	*/
	engine.POST("/login", func(context *gin.Context) {

		fmt.Println(context.FullPath())
		username, exist := context.GetPostForm("username")
		if exist {
			fmt.Println(username)
		}

		password, exists := context.GetPostForm("pwd")
		if exists {
			fmt.Println(password)
		}

		context.Writer.Write([]byte("Hello , " + username))
	})

	// 启动web引擎
	engine.Run(":8081")
}