Skip to content

Feat: Add Automatic HEAD Support for GET Routes#2944

Open
atharvaSharma17 wants to merge 2 commits intolabstack:masterfrom
atharvaSharma17:master
Open

Feat: Add Automatic HEAD Support for GET Routes#2944
atharvaSharma17 wants to merge 2 commits intolabstack:masterfrom
atharvaSharma17:master

Conversation

@atharvaSharma17
Copy link
Copy Markdown

@atharvaSharma17 atharvaSharma17 commented Apr 10, 2026

What this does

This adds an optional AutoHead flag that automatically enables HEAD requests for any GET route. No need to define separate HEAD handlers anymore.

Why this is useful

Right now, if you want proper HEAD support, you have to manually add a HEAD route for every GET route. That leads to:

  • Repeated boilerplate
  • Easy-to-miss HEAD routes (resulting in 405 errors)
  • Inconsistent headers like missing Content-Length

According to HTTP spec (RFC 7231), HEAD should behave like GET without a body — so this just makes Echo handle that for you.

How it works

When AutoHead is turned on:

  • Every GET route automatically gets a HEAD equivalent
  • The same handler runs, but the response body is suppressed
  • Headers and status code are preserved
  • Content-Length is still set correctly
  • If you’ve defined a HEAD route manually, it won’t be overridden

Implementation notes

  • Added AutoHead to Echo and Config
  • Wrapped handlers using a custom response writer
  • Hooked into route registration (add()) to register HEAD routes

Usage

e := echo.New()
e.AutoHead = true

e.GET("/api/users", handler)
// HEAD /api/users now works automatically

Performance

Very small overhead:

  • ~532 ns per request
  • 3 allocations

No impact if the feature is disabled (default).

Testing

Covers:

  • Enabled vs disabled behavior
  • Explicit HEAD route priority
  • Middleware compatibility

All tests pass.


Compatibility

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant