Replies: 1 comment 1 reply
|
For anyone hitting this today: the workaround is two lines and needs no framework change. 1. export const http = defineConfig({
allowMethodSpoofing: true,
})2. server.use([
() => import('#middleware/container_bindings_middleware'),
() => import('@adonisjs/static/static_middleware'),
() => import('@adonisjs/vite/vite_middleware'),
() => import('@adonisjs/core/bodyparser_middleware'), // <- moved here, last in the server stack
])
router.use([
// () => import('@adonisjs/core/bodyparser_middleware'), <- removed from here
() => import('@adonisjs/session/session_middleware'),
() => import('@adonisjs/shield/shield_middleware'),
() => import('@adonisjs/auth/initialize_auth_middleware'),
])That is all. Laravel-style Why it works. Caveat. Bodies are now parsed for requests that never match a route, 404s included. That is presumably why the default keeps the body parser on the router, and it is the one thing worth weighing before copying this. Tested on Why I still think it should be configurable. Right now every app migrating from Laravel has to rediscover this ordering detail and hand-edit its kernel, with no way to tell from the config that method spoofing reads the body at all. Supporting |
Uh oh!
There was an error while loading. Please reload this page.
Hi AdonisJS team! Thank you for all the work you put into the framework and its ecosystem. I am migrating a Laravel application to AdonisJS v7 and really appreciate the tools you provide.
Would you consider making the source of HTTP method spoofing explicitly configurable: query string, form body, or header?
Use case
Our migration shares existing frontend forms between Laravel and AdonisJS. Those forms submit POST requests with a hidden
_methodfield:The documented AdonisJS approach enables
allowMethodSpoofingand puts the override in the URL instead:The query-string approach works. An opt-in body source would make it easier to reuse existing form components during migrations without changing their transport convention or manually rearranging middleware.
Proposed API
Illustrative configuration in
config/app.ts:Suggested semantics:
query: read_methodfrom the query string.body: read_methodfrom the parsed form body, including URL-encoded and multipart forms.header: readX-HTTP-Method-Override.Selecting one source would make the behavior unambiguous when a request contains conflicting values. Existing applications using
allowMethodSpoofingshould keep their current behavior; spoofing should remain disabled by default, with query-string spoofing remaining the default when enabled without an explicit source. The configuration shape is only a suggestion.How other frameworks handle this
These are supported conventions, not a claim that every framework enables them by default.
_methodfield, e.g.<input type="hidden" name="_method" value="PUT">; Blade's@method('PUT')generates it._methodfield, e.g.<input type="hidden" name="_method" value="patch">._methodfield when form method overriding is enabled._methodin the parsed POST body, typically supplied by a hidden form field.HiddenHttpMethodFilterreads a form parameter named_methodby default.action="/users/1?_method=PUT"with a POST form.method-override_methodconvention. Method spoofing requires custom or third-party support.Express is a useful precedent for making the source selectable, although this capability comes from its
method-overridemiddleware, not Express core itself.Lifecycle considerations and prior discussion
I found #1116, where the maintainers explained that route selection happens before the body-parser middleware. I understand that body-based spoofing therefore involves request-lifecycle ordering, not just reading another field.
In our v7 application, enabling spoofing and registering the body parser in the server middleware stack before route matching allows the existing hidden-field forms to work. The request is for an officially supported configuration/integration for this behavior.
A body mode would need to preserve parser/upload limits and process the body only once. The effective method should be resolved consistently before routing and security middleware, with overrides restricted to original POST requests and an explicit set of allowed target methods such as PUT, PATCH, and DELETE.
Would an opt-in API along these lines fit AdonisJS's design? If a dedicated middleware integration is a better fit than core configuration, that would also address the use case.
Thank you again for maintaining AdonisJS and for considering the proposal!
All reactions