gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
https://gin-gonic.com/
MIT License
79.29k stars 8.03k forks source link

does gin context support delete certain query string in Request #3654

Open lancedang opened 1 year ago

lancedang commented 1 year ago

for example http://tt.com?x=name

I want to remove the x queryString for all incoming requests?does gin support this

thanks

yousifnimah commented 1 year ago

Hello @lancedang, you can use middleware to remove x queryString for all incoming requests. check the following code snippet below:

package main

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

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

    router.Use(RemoveQueryParam("x"))

    //Sample of route to check if x queryString has been removed
    router.GET("/", func(c *gin.Context) {
        c.JSON(200, c.Request.URL.Query())
    })

    router.Run(":8080")
}

// RemoveQueryParam Middleware to remove query string parameter
func RemoveQueryParam(param string) gin.HandlerFunc {
    return func(c *gin.Context) {
        values := c.Request.URL.Query()
        values.Del(param)
        c.Request.URL.RawQuery = values.Encode()
        c.Next()
    }
}