Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't readBody on a GET request #749

Open
julesfrn opened this issue May 9, 2024 · 0 comments
Open

Can't readBody on a GET request #749

julesfrn opened this issue May 9, 2024 · 0 comments

Comments

@julesfrn
Copy link

julesfrn commented May 9, 2024

Hello,

I am getting an error when I use the readBody() function on a get Request. After checking out the implementation I realised that it's a normal behaviour as it is specified in the code :

assertMethod(event, PayloadMethods);

I wonder why we want this specific behaviour, and not just return undefined like the function does for a POST request with no body.

I am not trying to make GET requests with a payload but I made a custom route array and registered them generically in my h3 router by looping on my array of routes so I use the readBody function for any routes no matter the verb.

const wrapHandler = (handler: HTTPHandler) => async (event: H3Event<EventHandlerRequest>) => {
    const request: HTTPRequest = {
      payload: await readBody(event),
      query: getQuery(event),
      params: getRouterParams(event)
    }
    const { status, body } = await handler(request)
    setResponseStatus(event, status)
    return body
  }

  routes.forEach(({ path, handler, method }) => {
    console.log(`Registering route : ${method} /api${path}`)
    nitroApp.router.use(`/api${path}`, defineEventHandler(wrapHandler(handler)), method)
  })

I feel like I should be able to do this but if not, do you have any suggestions on how I should handle this ?
I dont want to have to write an if ('post' || 'put' || 'patch'....) because I'm not supposed to know what h3 considers as a "PayloadMethod".
Maybe h3 should expose a method isPayloadMethod() so that i can use readBody() onfy if the previous function returns true

Thank you for your answers

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

No branches or pull requests

1 participant