route params dont seem to work for PUT or DELETE
Environment
in /server/api/[...slug].ts i have
const router = createRouter()
router.put('/user/:user/pen/:pen', update)
then my handler function has the following.
const update = defineEventHandler(async (event) => {
console.log(event.context.params)
....
which ends up showing:
{ _: 'user/1/pen/1' }
Reproduction
ill whip up a stackblitz asap and report back
Describe the bug
i should see context.params.id and context.params.user populated
Additional context
No response
Logs
No response
confirmed for DELETE as well
Thanks, please ping me when made reproduction but i would guess issue is from old radix3 behavior. If you can try against h3-nightly (v2) that would be amazing.
https://stackblitz.com/edit/github-zdmzwb?file=server%2Fapi%2F%5B...slug%5D.ts
I found that this was because I did not prepend the route with a /
import { createRouter, defineEventHandler, useBase } from 'h3';
const router = createRouter();
router.get(
'/**',
defineEventHandler((event) => 'not found')
);
router.put(
'user/:user/pen/:id',
defineEventHandler((event) => {
return {
parms: event.context.params,
};
})
);
export default useBase('/api/', router.handler);
returns :
data : {
"parms": {
"_": "user/1/pen/2"
}
}
while adding the slash:
import { createRouter, defineEventHandler, useBase } from 'h3';
const router = createRouter();
router.get(
'/**',
defineEventHandler((event) => 'not found')
);
router.put(
'/user/:user/pen/:id',
defineEventHandler((event) => {
return {
parms: event.context.params,
};
})
);
export default useBase('/api/', router.handler);
works fine:
data : {
"parms": {
"user": "1",
"id": "2"
}
}
i'm not sure if this is expected behavior, if it is im happy to close this
any update? i have trouble with this too
router.delete(
"/api/:delete",
defineEventHandler((event) => {
return getRouterParams(event)
})
);
router.get(
"/api/:get",
defineEventHandler((event) => {
return getRouterParams(event)
})
)
the router.get return key delete instead get
{
"delete": "asdasdasd"
}
I found that this was because I did not prepend the route with a /
@acidjazz Yes, routes should be registered with leading /.. Thanks for confirming.
@IqroNegoro, this an unfixable limitation with old radix3 which is resolved in rou3 (in upcoming h3 v2). The reason is that the old radix3 didn't have native HTTP method matching support.
Please consider using the same name of placeholders api/:id in current versions.
I found that this was because I did not prepend the route with a /
@acidjazz Yes, routes should be registered with leading
/.. Thanks for confirming.
@IqroNegoro, this an unfixable limitation with old radix3 which is resolved in rou3 (in upcoming h3 v2). The reason is that the old radix3 didn't have native HTTP method matching support.
Please consider using the same name of placeholders
api/:idin current versions.
Glad to hear that, so it'll be fixed in h3 v2 then?