Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kataras/methodoverride
:new: Lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it
https://github.com/kataras/methodoverride
go golang http methodoverride middleware nethttp
Last synced: 22 days ago
JSON representation
:new: Lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it
- Host: GitHub
- URL: https://github.com/kataras/methodoverride
- Owner: kataras
- License: mit
- Created: 2019-11-26T23:22:04.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-24T06:22:16.000Z (almost 2 years ago)
- Last Synced: 2024-08-03T23:29:08.237Z (3 months ago)
- Topics: go, golang, http, methodoverride, middleware, nethttp
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 7
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-golang-repositories - methodoverride
README
# HTTP Method Override (Go)
[![build status](https://img.shields.io/github/actions/workflow/status/kataras/methodoverride/ci.yml?style=for-the-badge)](https://github.com/kataras/methodoverride/actions) [![report card](https://img.shields.io/badge/report%20card-a%2B-ff3333.svg?style=for-the-badge)](https://goreportcard.com/report/github.com/kataras/methodoverride) [![godocs](https://img.shields.io/badge/go-%20docs-488AC7.svg?style=for-the-badge)](https://pkg.go.dev/github.com/kataras/methodoverride)
The use of specific custom HTTP headers such as X-HTTP methods override can be very handy while developing and promoting a REST API. When deploying REST API based web services, you may encounter access limitations on both the server and client sides.
**Some Firewalls do not support PUT, DELETE or PATCH requests.**
The `methodoverride` package is a [net/http](https://pkg.go.dev/net/http) middleware. **It lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it**.
## Getting started
The only requirement is the [Go Programming Language](https://go.dev/dl/).
```sh
$ go get github.com/kataras/methodoverride
``````go
package mainimport (
"net/http""github.com/kataras/methodoverride"
)func main() {
router := http.NewServeMux()mo := methodoverride.New(
// Defaults to nil.
//
methodoverride.SaveOriginalMethod("_originalMethod"),
// Default values.
//
// methodoverride.Methods(http.MethodPost),
// methodoverride.Headers("X-HTTP-Method",
// "X-HTTP-Method-Override",
// "X-Method-Override"),
// methodoverride.FormField("_method"),
// methodoverride.Query("_method"),
)router.HandleFunc("/path", func(w http.ResponseWriter, r *http.Request) {
resp := "post response"if r.Method == http.MethodDelete {
resp = "delete response"
}w.Write([]byte(resp))
})// Wrap your "router" with the methodoverride wrapper.
http.ListenAndServe(":8080", mo(router))
}```
A **client** can request with POST, the server will respond like if it were a DELETE method.
```js
fetch("/path", {
method: 'POST',
headers: {
"X-HTTP-Method": "DELETE"
},
})
.then((resp)=>{
// response body will be "delete response".
})).catch((err)=> { console.error(err) })
```## License
Methodoverride is free and open-source software licensed under the [MIT License](https://tldrlegal.com/license/mit-license).