methodoverride

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2022 License: MIT Imports: 5 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opt ...Option) func(next http.Handler) http.Handler

New returns a new method override wrapper which can be registered on any HTTP server.

Use this wrapper when you expecting clients that do not support certain HTTP operations such as DELETE or PUT for security reasons. This wrapper will accept a method, based on criteria, to override the POST method with.

Types

type GetterFunc

type GetterFunc func(http.ResponseWriter, *http.Request) string

GetterFunc is the type signature for declaring custom logic to extract the method name which a POST request will be replaced with.

type Option

type Option func(*options)

Option sets options for a fresh method override wrapper. See `New` package-level function for more.

func FormField

func FormField(fieldName string) Option

FormField specifies a form field to use to determinate the method to override the POST method with.

Example Field: <input type="hidden" name="_method" value="DELETE">

Defaults to: "_method".

func Getter

func Getter(customFunc GetterFunc) Option

Getter sets a custom logic to use to extract the method name to override the POST method with. Defaults to nil.

func Headers

func Headers(headers ...string) Option

Headers that client can send to specify a method to override the POST method with.

Defaults to: X-HTTP-Method X-HTTP-Method-Override X-Method-Override

func Methods

func Methods(methods ...string) Option

Methods can be used to add methods that can be overridden. Defaults to "POST".

func Only

func Only(o ...Option) Option

Only clears all default or previously registered values and uses only the "o" option(s).

The default behavior is to check for all the following by order: headers, form field, query string and any custom getter (if set). Use this method to override that behavior and use only the passed option(s) to determinate the method to override with.

Use cases:

  1. When need to check only for headers and ignore other fields: New(Only(Headers("X-Custom-Header")))

  2. When need to check only for (first) form field and (second) custom getter: New(Only(FormField("fieldName"), Getter(...)))

func Query

func Query(paramName string) Option

Query specifies a url parameter name to use to determinate the method to override the POST methos with.

Example URL Query string: http://localhost:8080/path?_method=DELETE

Defaults to: "_method".

func SaveOriginalMethod

func SaveOriginalMethod(requestContextKey interface{}) Option

SaveOriginalMethod will save the original method on Request.Context().Value(requestContextKey).

Defaults to nil, don't save it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL