gin_options.go 468 B

123456789101112131415161718
  1. package http_middleware
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. "strings"
  6. )
  7. // HTTP OPTIONS、HEAD 方法直接响应成功中间件
  8. func HandleOptionsMethod() gin.HandlerFunc {
  9. return func(ctxt *gin.Context) {
  10. if strings.ToUpper(ctxt.Request.Method) == http.MethodOptions || strings.ToUpper(ctxt.Request.Method) == http.MethodHead {
  11. ctxt.String(http.StatusOK, "")
  12. //ctxt.Header("Content-Type","text/html; charset=utf-8")
  13. ctxt.Abort()
  14. }
  15. }
  16. }