Last active
July 3, 2024 06:17
-
-
Save smiley-yoyo/72758c2c2fd461bd5357ebb9764366b9 to your computer and use it in GitHub Desktop.
parse query to map[string]any with gin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ( | |
| "github.com/gin-gonic/gin" | |
| ) | |
| type QueryMap map[string]any | |
| func ParseQuery(c *gin.Context) (QueryMap, error) { | |
| query := make(QueryMap) | |
| if c.Request.Method == "GET" { | |
| for k, v := range c.Request.URL.Query() { | |
| if len(v) == 1 { | |
| query[k] = v[0] | |
| } else { | |
| query[k] = v | |
| } | |
| } | |
| } else { | |
| // also try to parse from body | |
| if err := c.Bind(&query); err != nil { | |
| return nil, err | |
| } | |
| } | |
| return query, nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment