Context
Fox handlers use *fox.Context, a thin wrapper around Gin’s *gin.Context. It embeds Gin’s context, so Gin methods such as Query, Param, JSON, Abort, and GetHeader are available directly, while Fox adds request body caching, TraceID helpers, logger access, and context.Context compatibility.
Getting Context
Section titled “Getting Context”Fox handlers can optionally accept a *fox.Context parameter:
r.GET("/path", func(c *fox.Context) (any, error) { // Access context return response, nil})Gin middleware still uses *gin.Context:
func MyMiddleware() gin.HandlerFunc { return func(c *gin.Context) { c.Set("user_id", 123) c.Next() }}Request Data
Section titled “Request Data”Query Parameters
Section titled “Query Parameters”name := c.Query("name") // Get query parampage := c.DefaultQuery("page", "1") // With default valuePath Parameters
Section titled “Path Parameters”r.GET("/user/:id", func(c *gin.Context) { id := c.Param("id")})Headers
Section titled “Headers”token := c.GetHeader("Authorization")userAgent := c.Request.UserAgent()Request Body
Section titled “Request Body”var data map[string]anyc.ShouldBindJSON(&data)Fox also provides RequestBody() for reading and caching the raw request body. It restores the body so later binding can read it again:
body, err := c.RequestBody()Client IP
Section titled “Client IP”ip := c.ClientIP()Response
Section titled “Response”JSON Response
Section titled “JSON Response”c.JSON(200, gin.H{ "message": "success",})String Response
Section titled “String Response”c.String(200, "Hello, World!")HTML Response
Section titled “HTML Response”c.HTML(200, "index.html", gin.H{ "title": "Home",})Redirect
Section titled “Redirect”c.Redirect(302, "/new-path")Status Code
Section titled “Status Code”c.Status(204) // No ContentContext Data
Section titled “Context Data”Set Value
Section titled “Set Value”c.Set("user_id", 123)Get Value
Section titled “Get Value”userID := c.GetInt("user_id")value, exists := c.Get("key")Must Get
Section titled “Must Get”userID := c.MustGet("user_id").(int)File Handling
Section titled “File Handling”Single File Upload
Section titled “Single File Upload”file, _ := c.FormFile("file")c.SaveUploadedFile(file, "./uploads/"+file.Filename)Multiple Files
Section titled “Multiple Files”form, _ := c.MultipartForm()files := form.File["files"]
for _, file := range files { c.SaveUploadedFile(file, "./uploads/"+file.Filename)}Serve File
Section titled “Serve File”c.File("./assets/image.png")Cookies
Section titled “Cookies”Set Cookie
Section titled “Set Cookie”c.SetCookie( "session_id", // name "abc123", // value 3600, // maxAge (seconds) "/", // path "example.com", // domain false, // secure true, // httpOnly)Get Cookie
Section titled “Get Cookie”value, err := c.Cookie("session_id")Stop processing and return:
if !isAuthenticated(c) { c.JSON(401, gin.H{"error": "Unauthorized"}) c.Abort() return}Fox-Specific Methods
Section titled “Fox-Specific Methods”Get Logger
Section titled “Get Logger”c.Logger.Info("Processing request")Get TraceID
Section titled “Get TraceID”traceID := c.TraceID()Context Interface
Section titled “Context Interface”*fox.Context implements the standard context methods by delegating to c.Request.Context():
select {case <-c.Done(): return nil, c.Err()default:}
value := c.Value(myKey)Next Steps
Section titled “Next Steps”- Router API - Router configuration
- Middleware - Custom middleware