github.com/rotblauer/buffalo@v0.7.1-0.20170112214545-7aa55ef80dd3/method_override.go (about)

     1  package buffalo
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/markbates/going/defaults"
     7  )
     8  
     9  // MethodOverride can be be overridden to a user specified
    10  // function that can be used to change the HTTP Request Method.
    11  // var MethodOverride = MethodOverrideFunc
    12  var MethodOverride http.HandlerFunc
    13  
    14  // MethodOverrideFunc is the default implementation for the
    15  // MethodOverride. By default it will look for a form value
    16  // name `_method` and change the request method if that is
    17  // present and the original request is of type "POST". This is
    18  // added automatically when using `Automatic` Buffalo, unless
    19  // an alternative is defined in the Options.
    20  func MethodOverrideFunc(res http.ResponseWriter, req *http.Request) {
    21  	if req.Method == "POST" {
    22  		req.Method = defaults.String(req.FormValue("_method"), "POST")
    23  		req.Form.Del("_method")
    24  		req.PostForm.Del("_method")
    25  	}
    26  }