1. 程式人生 > 其它 >GET POST 以及獲取 Get Post 傳值

GET POST 以及獲取 Get Post 傳值

GET POST 以及獲取 Get Post 傳值

package main

import (
   "encoding/xml"
   "fmt"
   "github.com/gin-gonic/gin"
   "html/template"
   "net/http"
   "time"
)
type userInfo struct {
   //首字母大寫表示公有的
   Username string `json:"username" form:"username" `
   Password string `json:"password" form:"password" `
}
type Article struct {
   Title   string `json:"title" xml:"title"`
   Content string `json:"content" xml:"content"`
}


// UnixToTime 時間戳轉換成日期
func UnixToTime(timestamp int) string {
   fmt.Println(timestamp)
   t := time.Unix(int64(timestamp), 0)
   return t.Format("2006-01-02 15:04:05")
}
func Println(str1 string, str2 string) string {
   fmt.Println(str1, str2)

   return str1 + "-----" + str2
}

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

   //配置自定義模板函式
   r.SetFuncMap(template.FuncMap{
      //註冊模板函式
      "UnixToTime": UnixToTime,
      "Println":    Println,
   })
   //配置模板檔案的路徑,放在路由配置的前面:載入templates下的所有檔案
   r.LoadHTMLGlob("templates/**/*")

   //靜態資源,第一個引數是路由,第二個是本地的目錄
   r.Static("/static", "./static")
   //Get 請求傳值
   //請求方式:http://localhost:8080/?username=1&age=12&password=123
   r.GET("/", func(c *gin.Context) {
      username :=c.Query("username")
      age :=c.Query("age")
      //DefaultQuery:有返回值賦值,不存在給預設值1
      page :=c.DefaultQuery("page","1")
      password :=c.Query("password")
      c.JSON(http.StatusOK,gin.H{
         "username":username,
         "age":age,
         "page":page,
         "password":password,
      })
   })
   //Get請求傳值 id
   //http://localhost:8080/article?id=12
   r.GET("/article", func(c  *gin.Context) {
      id := c.DefaultQuery("id","1")
      c.JSON(http.StatusOK,gin.H{
         "mag":"新聞詳情",
         "id":id,
      })
   })

   //post演示
   //http://localhost:8080/article?id=12
   r.GET("/user", func(c  *gin.Context) {
      c.HTML(http.StatusOK,"default/user.html",gin.H{})
   })
   r.POST("/doAddUser", func(c  *gin.Context) {
      //獲取表單post過來的資料:一個一個獲取
      username :=c.PostForm("username")
      password :=c.PostForm("password")
      age :=c.DefaultPostForm("age","20")
      c.JSON(http.StatusOK,gin.H{
         "username":username,
         "password":password,
         "age":age,
      })
   })

   //獲取 GET POST 傳遞的資料繫結到結構體
   r.GET("/getUser", func(c  *gin.Context) {
      //例項化一個結構體
      user :=&userInfo{}
      //ShouldBind:必須傳入地址
      if err := c.ShouldBind(&user);err ==nil{
         //成功
         fmt.Printf("%#v\n", user)//列印結構體資料
         c.JSON(http.StatusOK,user)
      }else {
         //失敗
         c.JSON(http.StatusOK,gin.H{
            "err":err.Error(),
         })
      }
   })

   //獲取 Post Xml 資料
   r.POST("/xml", func(c  *gin.Context) {
      //例項化一個結構體
      article :=&Article{}
      xmlSliceData, _ := c.GetRawData() // 從 c.Request.Body 讀取xml請求資料
      if err := xml.Unmarshal(xmlSliceData,article);err ==nil{
         //成功
         c.JSON(http.StatusOK,article)
      }else {
         //失敗
         c.JSON(http.StatusBadRequest,gin.H{
            "err":err.Error(),
         })
         }})

   //動態路由傳值
   //localhost:8080/list/123
   r.GET("/list/:cid", func(c  *gin.Context) {
      cid := c.Param("cid")
      c.JSON(http.StatusOK,gin.H{
         "cid":cid,
      })
   })
   r.Run()

}

獲取 Post Xml 資料:使用場景,支付介面

獲取xml資料,使用Unmarshal解析到結構體中,然後轉換為json格式返回給前臺