gin-http-method-override

A Gin web framework middleware that allows you to override the HTTP method of a request through the X-HTTP-Method-Override header.

Learn more about X-HTTP-Method-Override:

  1. Overriding security restrictions of HTTP methods – IBM Documentation
  2. HTTP PUT or DELETE not allowed? Use X-HTTP-Method-Override for your REST Service with ASP.NET Web API – Scott Hanselman’s Blog
  3. 详解 RESTful API 中的动词覆盖 – 知乎

Use this middleware in the senario where only POST requests are allowed to be sent while the backend service still need to maintenan the REST standards.

Usage

Server side

package main

import (
    "net/http"

    "github.com/gin-gonic/gin"
    "github.com/nekomeowww/gin-http-method-override"
)

func main() {
	r := gin.Default()
	r.Use(ginhttpmethodoverride.New(r))
	r.PUT("/", func(c *gin.Context) {
		c.String(http.StatusOK, "Hello World")
	})

	r.Run(":8080")
}

Client side

import (
    "net/http"

    "github.com/nekomeowww/gin-http-method-override"
)

func doRequest() {
	request, err := http.NewRequest(http.MethodPost, "/", nil)
	if err != nil {
		log.Println(err)
	}

	request.Header = http.Header{}
	request.Header.Set(ginhttpmethodoverride.XHTTPMethodOverrideHeader, "PUT")
}

GitHub

View Github