Skip to main content

Overview

The methodoverride middleware allows HTML forms to use HTTP methods other than GET and POST. Since HTML forms only support GET and POST, this middleware lets you override the method using a header or form field.

Installation

Quick Start

Configuration

Examples

HTML Form with PUT

DELETE via Form

Header Override (AJAX)

Custom Configuration

How It Works

  1. Only processes POST requests
  2. Checks header, then query, then form field for override value
  3. If valid method found, changes request method
  4. Subsequent handlers see the overridden method

API Reference

Technical Details

Implementation Overview

The method override middleware works by intercepting POST requests and checking for method override indicators in the following priority order:
  1. Header Check: First checks the override header (default: X-HTTP-Method-Override)
  2. Query Parameter: If no header is found, checks the query parameter with the form field name
  3. Form Field: For form submissions (content types application/x-www-form-urlencoded or multipart/form-data), checks the form field value

Method Validation

  • Override values are converted to uppercase for case-insensitive matching
  • Only methods specified in the Methods configuration are allowed (default: PUT, PATCH, DELETE)
  • Invalid or disallowed methods are silently ignored, leaving the original POST method intact
  • The allowed methods are stored in a map for O(1) lookup performance

Request Processing

The middleware only processes POST requests. Any other HTTP method (GET, PUT, DELETE, etc.) passes through unchanged, even if override headers or parameters are present. This prevents potential security issues from unexpected method changes.

Content Type Handling

Form field checking only occurs when the request has an appropriate content type:
  • application/x-www-form-urlencoded
  • multipart/form-data
This ensures form parsing is only attempted for actual form submissions, avoiding unnecessary processing for JSON or other content types.

Best Practices

Use CSRF Protection

Always combine method override with CSRF protection when handling form submissions:

Restrict Allowed Methods

Only enable the HTTP methods your application actually needs:

Header Priority

When using AJAX requests, prefer the header approach for cleaner code and better separation from form data.

Testing

The middleware includes comprehensive test coverage for all override scenarios:

Security Notes

  • Only POST requests can be overridden
  • Only allowed methods (PUT, PATCH, DELETE by default) can be used
  • Use with CSRF protection for forms
  • csrf - Form protection