Categorygithub.com/gin-contrib/size
modulepackage
1.0.1
Repository: https://github.com/gin-contrib/size.git
Documentation: pkg.go.dev

# README

size

Run Tests codecov Go Report Card GoDoc Join the chat at https://gitter.im/gin-gonic/gin

Limit size of POST requests for Gin framework

Example

package main

import (
  "net/http"

  limits "github.com/gin-contrib/size"
  "github.com/gin-gonic/gin"
)

func handler(ctx *gin.Context) {
  val := ctx.PostForm("b")
  if len(ctx.Errors) > 0 {
    return
  }
  ctx.String(http.StatusOK, "got %s\n", val)
}

func main() {
  r := gin.Default()
  r.Use(limits.RequestSizeLimiter(10))
  r.POST("/", handler)
  if err := r.Run(":8080"); err != nil {
    log.Fatal(err)
  }
}

# Functions

RequestSizeLimiter returns a middleware that limits the size of request When a request is over the limit, the following will happen: * Error will be added to the context * Connection: close header will be set * Error 413 will be sent to the client (http.StatusRequestEntityTooLarge) * Current context will be aborted.