Skip to content

feat: middleware raw body#1506

Merged
ernado merged 4 commits intoogen-go:mainfrom
Sosorevgm:feat/middleware-raw-body
Sep 18, 2025
Merged

feat: middleware raw body#1506
ernado merged 4 commits intoogen-go:mainfrom
Sosorevgm:feat/middleware-raw-body

Conversation

@Sosorevgm
Copy link
Contributor

@Sosorevgm Sosorevgm commented Aug 3, 2025

Add raw request body to middleware.Request for signature validation use cases

This PR adds a new field RawBody []byte to the middleware.Request struct, allowing middleware to access the raw HTTP request body.

Motivation

In some scenarios — such as validating HMAC or SHA-based signatures — it is necessary to work with the exact raw body of the incoming request. Currently, ogen consumes the request body during decoding, and does not expose it to middleware.

Example

Demo project here

Update:

I found an existing PR that introduces a similar idea: #1505, where the author recovers the body using io.NopCloser.

The goal is the same - to have access to the original body.

Compared to that approach, this PR has the following advantages:
• No need to manually defer r.Body.Close() or wrap the request body with io.NopCloser.
• Middleware receives the raw body directly via middleware.Request.RawBody, so there’s no need to re-read or re-wrap the body again.

This approach keeps the original request lifecycle untouched and adds the raw body as a read-only byte slice for middleware purposes.

@Sosorevgm
Copy link
Contributor Author

Hi @ernado, could you please take a look when you have time?

@ernado
Copy link
Member

ernado commented Sep 16, 2025

@Sosorevgm can you please rebase to trigger CI/CD pipeline

@Sosorevgm Sosorevgm force-pushed the feat/middleware-raw-body branch from 80ca464 to 79e292b Compare September 17, 2025 08:06
@ernado
Copy link
Member

ernado commented Sep 17, 2025

Can you please

  1. Rebase, e.g. git pull --rebase origin main
  2. make generate examples

@ernado ernado merged commit 801335b into ogen-go:main Sep 18, 2025
15 checks passed
@Sosorevgm Sosorevgm deleted the feat/middleware-raw-body branch September 19, 2025 12:55
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.

2 participants