go : gin Urlencoded 格式

背景:
本文介紹 Urlencoded 自定義格式

代碼:

package main

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

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

	router.POST("/form_post", func(c *gin.Context) {
		message := c.PostForm("message")
		nick := c.DefaultPostForm("nick", "anonymous")

		c.JSON(200, gin.H{
			"status":  "posted",
			"message": message,
			"nick":    nick,
		})
	})
	router.Run(":8080")
}

運行程序
客戶端測試

->curl  localhost:8080/form_post --form message=hello
{"message":"hello","nick":"anonymous","status":"posted"}

服務端日誌
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章