zoukankan      html  css  js  c++  java
  • gin获取全部参数

    原文链接:https://blog.csdn.net/keyunq/article/details/82226280
    
    
    一直都是用结构体接收参数,假如事先不清楚参数名,或者参数是不固定的,就要动态获取。
    
    ctx *gin.Context
    
    form方式的请求:
    
        ctx.Request.ParseForm()
        for k, v := range ctx.Request.PostForm {
            fmt.Printf("k:%v
    ", k)
            fmt.Printf("v:%v
    ", v)
        }
    
    json方式的请求:
    
    data, _ := ioutil.ReadAll(ctx.Request.Body)
    fmt.Printf("ctx.Request.body: %v", string(data))
    1
    2
    然后从data里解析出来
    
    代码段记录
    
    logging.Debugf("c.Request.Method: %v", ctx.Request.Method)
    logging.Debugf("c.Request.ContentType: %v", ctx.ContentType())
    
    logging.Debugf("c.Request.Body: %v", ctx.Request.Body)
    ctx.Request.ParseForm()
    logging.Debugf("c.Request.Form: %v", ctx.Request.PostForm)
    
    for k, v := range ctx.Request.PostForm {
        logging.Debugf("k:%v
    ", k)
        logging.Debugf("v:%v
    ", v)
    }
    
    logging.Debugf("c.Request.ContentLength: %v", ctx.Request.ContentLength)
    data, _ := ioutil.ReadAll(ctx.Request.Body)
    
    logging.Debugf("c.Request.GetBody: %v", string(data))
    
    
    //第二种:
    package main
    
    import (
    	"fmt"
    	"net/http"
    
    	"github.com/gin-gonic/gin"
    )
    
    func main() {
    	router := gin.Default()
    	router.POST("/events", events)
    	router.Run(":5000")
    }
    
    func events(c *gin.Context) {
    	buf := make([]byte, 1024)
    	n, _ := c.Request.Body.Read(buf)
    	fmt.Println(string(buf[0:n]))
    	resp := map[string]string{"hello": "world"}
    	c.JSON(http.StatusOK, resp)
    	/*post_gwid := c.PostForm("name")
    	fmt.Println(post_gwid)*/
    
    
    
  • 相关阅读:
    hive 修复分区、添加二级分区
    hive sql 查询一张表的数据不在另一张表中
    shell 命令 bc linux下的计算器
    shell 命令 grep -v
    shell 命令 -- 漂亮的资源查看命令 htop
    shell 命令 --ps aux | grep
    presto调研和json解析函数的使用
    shell wc -l
    hive 动态分区与混合分区
    ThreadLocal原理分析与使用场景(转)
  • 原文地址:https://www.cnblogs.com/lajiao/p/11769507.html
Copyright © 2011-2022 走看看