Skip to content
/ chme Public

Middlewares that enable HTML form to work more RESTful

License

Notifications You must be signed in to change notification settings

tomocy/chme

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chme

CircleCI License: MIT

Chme provides middlewares which enable you to do requests with other methods than GET and POST from HTML form.
Chme stands for "change methods".

Description

HTML form does not support methods except GET and POST.
But you sometimes want it to request with more proper methods such as when we provide some edit feature.

Usage

The signature of middlewares in this repository is the standard one.

func(next http.Handler) http.Handler

ChangePostToHiddenMethod

The usage is very simple.
All you have to do is add input named "_method" in POST form.

<form action="/articles/1" method="POST">
    <input type="hidden" name="_method" value="PUT">
</form>

And in Go, you can handle the request as if it is done with the method set in the input. (chi is used as an example.)

r := chi.NewRouter()
r.Use(ChangePostToHiddenMethod)
r.Put("/articles/{id}", func(w http.ResponseWriter, r *http.Request) {
    // do something
})

You can change POST to PUT, PATCH, and DELETE.
Make sure to specify html form method as POST because the default method of HTML form is GET and this function ignores requests with GET.

You can change default input name "_method" to anything you want.

// Change to "other" for the input
r.Use(NewChme("other").ChangePostToHiddenMethod)

Install

go get github.com/tomocy/chme

Licence

MIT

Author

tomocy

About

Middlewares that enable HTML form to work more RESTful

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages