HTTP Method Override
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 Method Override wrapper lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it.

Server

1
package main
2
3
import (
4
"github.com/kataras/iris/v12"
5
"github.com/kataras/iris/v12/middleware/methodoverride"
6
)
7
8
func main() {
9
app := iris.New()
10
11
mo := methodoverride.New(
12
// Defaults to nil.
13
//
14
methodoverride.SaveOriginalMethod("_originalMethod"),
15
// Default values.
16
//
17
// methodoverride.Methods(http.MethodPost),
18
// methodoverride.Headers("X-HTTP-Method",
19
// "X-HTTP-Method-Override",
20
// "X-Method-Override"),
21
// methodoverride.FormField("_method"),
22
// methodoverride.Query("_method"),
23
)
24
// Register it with `WrapRouter`.
25
app.WrapRouter(mo)
26
27
app.Post("/path", func(ctx iris.Context) {
28
ctx.WriteString("post response")
29
})
30
31
app.Delete("/path", func(ctx iris.Context) {
32
ctx.WriteString("delete response")
33
})
34
35
// [...app.Run]
36
}
Copied!

Client

1
fetch("/path", {
2
method: 'POST',
3
headers: {
4
"X-HTTP-Method": "DELETE"
5
},
6
})
7
.then((resp)=>{
8
// response body will be "delete response".
9
})).catch((err)=> { console.error(err) })
Copied!
Export as PDF
Copy link
Contents
Server
Client