Skip to content

🆕 Lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it

License

Notifications You must be signed in to change notification settings

kataras/methodoverride

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTTP Method Override (Go)

build status report card godocs

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 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.

$ go get github.com/kataras/methodoverride
package main

import (
    "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.

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.

About

🆕 Lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages