Skip to content

Feat: Add Automatic HEAD Support for GET Routes#2944

Closed
atharvaSharma17 wants to merge 3 commits into
labstack:masterfrom
atharvaSharma17:master
Closed

Feat: Add Automatic HEAD Support for GET Routes#2944
atharvaSharma17 wants to merge 3 commits into
labstack:masterfrom
atharvaSharma17:master

Conversation

@atharvaSharma17

@atharvaSharma17 atharvaSharma17 commented Apr 10, 2026

Copy link
Copy Markdown

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

Comment thread echo.go Outdated
vishr added a commit that referenced this pull request Jun 13, 2026
- Correct net/http interop claim: Echo handlers are not http.Handler;
  interop is via WrapHandler/WrapMiddleware
- Clarify binding ships with a pluggable validator (not built-in validation)
- ROADMAP: reference canonical auto-HEAD issue/PR (#2895/#2949) instead of
  the duplicate PRs (#2944/#2937) slated for closure

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
vishr added a commit that referenced this pull request Jun 13, 2026
* docs: add liveness signals to README and a public ROADMAP

- Add dynamic latest-release and last-commit badges (can't go stale)
- Add positioning vs net/http and an "actively maintained" note
- Add ROADMAP.md with version policy (v5 current, v4 LTS to 2026-12-31)
  and surface it from the README

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* docs: fix review findings in README/ROADMAP

- Correct net/http interop claim: Echo handlers are not http.Handler;
  interop is via WrapHandler/WrapMiddleware
- Clarify binding ships with a pluggable validator (not built-in validation)
- ROADMAP: reference canonical auto-HEAD issue/PR (#2895/#2949) instead of
  the duplicate PRs (#2944/#2937) slated for closure

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vishr

vishr commented Jun 13, 2026

Copy link
Copy Markdown
Member

Thank you for tackling this — automatic HEAD support has been one of our most-requested features (#2895). We're consolidating the work into #2949, which takes a router-level approach with an opt-in hook so users keep control. Closing this in favor of that PR, but your work helped shape the direction and it's genuinely appreciated. Please follow and review #2949. 🙏

@vishr vishr closed this Jun 13, 2026
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.

3 participants