gofiber / fiber

⚡️ Express inspired web framework written in Go
https://gofiber.io
MIT License
33.23k stars 1.64k forks source link

🤗 [Question]: Can not pass query params in group prefix #2009

Closed wavemoroc001 closed 2 years ago

wavemoroc001 commented 2 years ago

Question Description

Hi there, I try to pass query params in nested url prefix like this

http://localhost:8585/classroom-service/account/submit-otp-link?test=testMessage

Router

func (h *Handler) Register(r *fiber.App) {
    v1 := r.Group("classroom-service")
    account := v1.Group("/account")
    account.Get("/submit-otp-link", h.handlePassQuery)
}

Handler

func (h *Handler) handlePassQuery(c *fiber.Ctx) error {
    param := c.Query("test", "Noting Here")
    return c.Status(fiber.StatusOK).JSON(fiber.Map{
        "Message": param,
    })
}

But it throw this Response

{
    "Reason": "strconv.ParseUint: parsing \"submit-otp-link\": invalid syntax",
    "Status": 400,
    "Timestamp": "2022-08-09 14:42:17.836431 TZ+07"
}

Cloud you advice how to fix this errors ?

Fiber version : 2.36.0

Code Snippet (optional)

No response

Checklist:

welcome[bot] commented 2 years ago

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

ReneWerner87 commented 2 years ago
package main

import (
    "log"

    "github.com/gofiber/fiber/v2"
)

func handlePassQuery(c *fiber.Ctx) error {
    param := c.Query("test", "Noting Here")
    return c.Status(fiber.StatusOK).JSON(fiber.Map{
        "Message": param,
    })
}

func main() {
    app := fiber.New()

    v1 := app.Group("classroom-service")
    account := v1.Group("/account")
    account.Get("/submit-otp-link", handlePassQuery)

    log.Fatal(app.Listen(":8585"))
}

image image

@wavemoroc001 i cannot reproduce ? can't find "strconv.ParseUint" anywhere in our code, so must be the processes you didn't specify in the example

wavemoroc001 commented 2 years ago

@ReneWerner87 Thank you for you kindness I found root cause, I think that might match route to getAccountByID that i use strconv before go to submit-otp-link route

To fix that, Should I declare route submit-otp-link before GetAccountByID or Any better solution to prevent fiber route to wrong endpoint ?

    account.Get("/:ID", h.GetAccountByID)
    account.Get("/submit-otp-link", h.handlePassQuery)
ReneWerner87 commented 2 years ago

image https://docs.gofiber.io/guide/routing

yes please swap the route with the variable part with the route with the fixed part, otherwise the variable part will always be used before the other route, because with us the order plays a role just like with express

wavemoroc001 commented 2 years ago

Thank you so much !