Categorygithub.com/bu/gin-method-override
modulepackage
0.0.0-20210107045130-0be2f37312dc
Repository: https://github.com/bu/gin-method-override.git
Documentation: pkg.go.dev

# README

Gin MethodOverride Middleware

Go Report Card Build Status

A Gin web framework middleware for method override by POST form param _method, inspired by Ruby's same name rack

Usage

Server-side


package main

import (
	gin "github.com/gin-gonic/gin"
	method "github.com/bu/gin-method-override"
)

func main() {
	// create a Gin engine
	r := gin.Default()

	// our middle-ware
	r.Use(method.ProcessMethodOverride(r))

	// routes
	r.PUT("/test", func (c *gin.Context) {
		c.String(200, "1")
	})

	// listen to request
	r.Run(":8080")
}

Client side

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
    <form action="/test" method="POST"> <!-- form method rewrite only works with POST request -->
        <input type="hidden" name="_method" value="PUT">
        <input type="text" name="testing" value="1">
        <button type="submit">Send</button>
    </form>
</body>
</html>

# Functions

ProcessMethodOverride check POST request and re-dispatch them if _method exists in POST body, this was intent to act as Ruby MethodOverride rack ref: http://www.rubydoc.info/gems/rack/Rack/MethodOverride.