Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST Method override #1325

Closed
Dexus opened this issue Aug 4, 2019 · 7 comments
Closed

POST Method override #1325

Dexus opened this issue Aug 4, 2019 · 7 comments

Comments

@Dexus
Copy link

Dexus commented Aug 4, 2019

Hello @kataras,

would it be possible to add a optional "Method Override"?

Like https://www.npmjs.com/package/method-override or http://fandry.blogspot.com/2012/03/x-http-header-method-override-and-rest.html

While some Firewalls do not support PUT, DELETE or PATCH request, I would like to know what would the best way to implement it for requirements like this.

Best Regards, Josef

@kataras
Copy link
Owner

kataras commented Aug 5, 2019

Hello Josef,

It can be implemented as a Router Wrapper. I can write a small builtin wrapper for that later on today.

@kataras
Copy link
Owner

kataras commented Aug 5, 2019

Just to be clear on this @Dexus , you want something like this, right?:

app := iris.New()

app.Post("/path", func(ctx iris.Context){
    // handle post
   ctx.WriteString("post")
})

app.Delete("/path", func(ctx iris.Context){
    // handle DELETE or POST with X-HTTP-Method == 'DELETE'
    //  -> req.Method = DELETE
   ctx.WriteString("delete")
})
fetch("/path", {
    method: 'POST',
    headers: {
      "X-HTTP-Method": "DELETE"
    },
  })
  .then((resp)=>{
       //  resp == "delete" ? // <-------------
 })).catch(...)

@Dexus
Copy link
Author

Dexus commented Aug 5, 2019

That's exactly what I mean. I don't know if you can do the control via form field as well. In the past, with Nginx and Apache, I had both the header and the form variant (input called _method).
Thank you very much.

@kataras
Copy link
Owner

kataras commented Aug 5, 2019

Nice! Don't worry, it will be based on the resources you posted above:

  • form field
  • url query
  • header (x-http-method, x-method-override, x-http-method-override)
  • custom getter

@kataras
Copy link
Owner

kataras commented Aug 6, 2019

@Dexus it's done. Please test it in your app whenever you have time.

import "github.com/kataras/iris/middleware/methodoverride"

Test (&Example): https://github.com/kataras/iris/blob/master/middleware/methodoverride/methodoverride_test.go.

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)

Override all default values with: methodoverride.New(methodoverride.Only(options...))

@Dexus
Copy link
Author

Dexus commented Aug 6, 2019

Works as expected and now makes it easier to use appropriate APIs under firewall fire. Thank you very much @kataras!

@Dexus Dexus closed this as completed Aug 6, 2019
@kataras
Copy link
Owner

kataras commented Aug 6, 2019

Nice! You are welcome @Dexus! I thank you for the feature request

github-actions bot pushed a commit to goproxies/github.aaakk.us.kg-kataras-iris that referenced this issue Jul 27, 2020
Former-commit-id: e476998d8d6b10dc641b3393577937269adb694c
github-actions bot pushed a commit to goproxies/github.aaakk.us.kg-kataras-iris that referenced this issue Jul 27, 2020
Former-commit-id: 9febac1ba701b29e59d7660540f03217f12cf9e0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants