h3 icon indicating copy to clipboard operation
h3 copied to clipboard

route params dont seem to work for PUT or DELETE

Open acidjazz opened this issue 1 year ago • 3 comments

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

acidjazz avatar Aug 13 '24 06:08 acidjazz

confirmed for DELETE as well

acidjazz avatar Aug 16 '24 04:08 acidjazz

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.

pi0 avatar Aug 16 '24 20:08 pi0

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

acidjazz avatar Aug 17 '24 07:08 acidjazz

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"
}

IqroNegoro avatar Mar 08 '25 06:03 IqroNegoro

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.

pi0 avatar Apr 15 '25 11:04 pi0

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.

Glad to hear that, so it'll be fixed in h3 v2 then?

IqroNegoro avatar Apr 15 '25 11:04 IqroNegoro