mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
add http method override section
parent
739a61e8bd
commit
35dd6212f7
60
HTTP-method-override.md
Normal file
60
HTTP-method-override.md
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
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](https://github.com/kataras/iris/tree/master/middleware/methodoverride) wrapper lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it.
|
||||||
|
|
||||||
|
### Server
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kataras/iris/v12"
|
||||||
|
"github.com/kataras/iris/v12/middleware/methodoverride"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app := iris.New()
|
||||||
|
|
||||||
|
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"),
|
||||||
|
)
|
||||||
|
// Register it with `WrapRouter`.
|
||||||
|
app.WrapRouter(mo)
|
||||||
|
|
||||||
|
app.Post("/path", func(ctx iris.Context) {
|
||||||
|
ctx.WriteString("post response")
|
||||||
|
})
|
||||||
|
|
||||||
|
app.Delete("/path", func(ctx iris.Context) {
|
||||||
|
ctx.WriteString("delete response")
|
||||||
|
})
|
||||||
|
|
||||||
|
// [...app.Run]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Client
|
||||||
|
|
||||||
|
```js
|
||||||
|
fetch("/path", {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
"X-HTTP-Method": "DELETE"
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then((resp)=>{
|
||||||
|
// response body will be "delete response".
|
||||||
|
})).catch((err)=> { console.error(err) })
|
||||||
|
```
|
1
Home.md
1
Home.md
|
@ -21,6 +21,7 @@ This wiki is the main source of documentation for **developers** working with (o
|
||||||
* [[Wrap the Router|Routing-wrap-the-router]]
|
* [[Wrap the Router|Routing-wrap-the-router]]
|
||||||
* [[Override Context|Routing-override-context]]
|
* [[Override Context|Routing-override-context]]
|
||||||
* [[Context Methods|Routing-context-methods]]
|
* [[Context Methods|Routing-context-methods]]
|
||||||
|
* [[HTTP Method Override]]
|
||||||
* [[API Versioning]]
|
* [[API Versioning]]
|
||||||
* [[Content Negotiation]]
|
* [[Content Negotiation]]
|
||||||
* [[Response Recorder]]
|
* [[Response Recorder]]
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
* [[Wrap the Router|Routing-wrap-the-router]]
|
* [[Wrap the Router|Routing-wrap-the-router]]
|
||||||
* [[Override Context|Routing-override-context]]
|
* [[Override Context|Routing-override-context]]
|
||||||
* [[Context Methods|Routing-context-methods]]
|
* [[Context Methods|Routing-context-methods]]
|
||||||
|
* [[HTTP Method Override]]
|
||||||
* [[API Versioning]]
|
* [[API Versioning]]
|
||||||
* [[Content Negotiation]]
|
* [[Content Negotiation]]
|
||||||
* [[Response Recorder]]
|
* [[Response Recorder]]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user