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

🤗 [Question]: v3 bind query and json make a mistake #3253

Closed
3 tasks done
cccqcccq opened this issue Dec 17, 2024 · 6 comments
Closed
3 tasks done

🤗 [Question]: v3 bind query and json make a mistake #3253

cccqcccq opened this issue Dec 17, 2024 · 6 comments

Comments

@cccqcccq
Copy link

cccqcccq commented Dec 17, 2024

Question Description

I'm having an issue when I need to bind both query and json in v3

type ProductListReq struct {
	Page   int32  `json:"-"`
	Size   int32  `json:"-"`
	Search string `json:"search"`
}

func (u *Product) List(c fiber. Ctx) error {
	var req ProductListReq
	if err := c.Bind(). Query(&req); err != nil {
		return u.Error(c, fiber. StatusBadRequest, utils. Translate(err))
	}
	if c.Get("Content-Type") == "application/json" {
		if err := c.Bind(). JSON(&req); err != nil {
			return u.Error(c, fiber. StatusBadRequest, utils. Translate(err))
		}
	}
	return nil
}

This is the correct code, but when I remove it
if c.Get("Content-Type") == "application/json"
I'm getting an error:invalid character '' looking for beginning of value

Code Snippet (optional)

No response

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.
Copy link

welcome bot commented Dec 17, 2024

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

@gaby
Copy link
Member

gaby commented Dec 20, 2024

@efectn Any ideas?

@cccqcccq
Copy link
Author

It should be added that json is also passed in the query, for example: url?page=1&size=9&search={keyword:'key'}

@ReneWerner87
Copy link
Member

I have just tried it with beta4 and do not get the error

to be able to reproduce this better I would ask you(@cccqcccq ) to provide a curl request and a golang snippet to reproduce it

@cccqcccq
Copy link
Author

cccqcccq commented Jan 1, 2025

我刚刚用 beta4 尝试过,但没有收到错误

为了能够更好地重现这一点,我请求您( )提供一个 curl 请求和一个 golang 片段来重现它

package main

import (
	"github.com/gofiber/fiber/v3"
)

type Product struct {
}

type ProductListReq struct {
	Page   int32  `json:"page"`
	Size   int32  `json:"size"`
	Search string `json:"search"`
}

func (p *Product) List(c fiber.Ctx) error {
	var req ProductListReq
	if err := c.Bind().Query(&req); err != nil {
		return p.Error(c, fiber.StatusBadRequest, err.Error())
	}
	if err := c.Bind().JSON(&req); err != nil {
		return p.Error(c, fiber.StatusBadRequest, err.Error())
	}
	return p.Respond(c, fiber.StatusOK, req, 0, "Success")
}

func main() {
	app := fiber.New()
	p := Product{}
	app.Get("/", p.List)
	app.Listen(":8080")
}

type Response struct {
	Code  int         `json:"code"`
	Data  interface{} `json:"data,omitempty"`
	Count int32       `json:"count,omitempty"`
	Msg   string      `json:"msg"`
}

func (p *Product) Respond(c fiber.Ctx, code int, data interface{}, count int32, msg string) error {
	if msg == "" {
		if code == fiber.StatusOK {
			msg = "Success"
		} else {
			msg = "Failed to process request"
		}
	}

	return c.Status(code).JSON(Response{
		Code:  code,
		Data:  data,
		Count: count,
		Msg:   msg,
	})
}
func (p *Product) Error(c fiber.Ctx, code int, msg ...string) error {
	defaultMsg := "Failed to process request"
	if len(msg) > 0 {
		defaultMsg = msg[0]
	}
	return p.Respond(c, code, nil, 0, defaultMsg)
}

The search parameter is a JSON
Now return unexpected end of JSON input
image

curl --location --request GET 'http://127.0.0.1:8080/?page=1&size=9&search=%7B%22name%22:%22Mike%22%7D' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Accept: */*' \
--header 'Host: 127.0.0.1:8080' \
--header 'Connection: keep-alive'

When I modified it to the following code

func (p *Product) List(c fiber.Ctx) error {
	var req ProductListReq
	if err := c.Bind().Query(&req); err != nil {
		return p.Error(c, fiber.StatusBadRequest, err.Error())
	}
        // Added this judgment
	if c.Get("Content-Type") == "application/json" {
		if err := c.Bind().JSON(&req); err != nil {
			return p.Error(c, fiber.StatusBadRequest, err.Error())
		}
	}
	return p.Respond(c, fiber.StatusOK, req, 0, "Success")
}

This is the correct return value
Thank you for your willingness to help me
image

@cccqcccq cccqcccq closed this as completed Jan 1, 2025
@cccqcccq
Copy link
Author

cccqcccq commented Jan 1, 2025

It's my mistake.,After adding the judgment, it leads to no binding json so that it can be executed correctly.,Just use Query to bind correctly.
I'm sorry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants