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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃 [Question]: How to use BodyParse in v3 ? #2964

Open
3 tasks done
root9464 opened this issue Apr 13, 2024 · 3 comments
Open
3 tasks done

馃 [Question]: How to use BodyParse in v3 ? #2964

root9464 opened this issue Apr 13, 2024 · 3 comments

Comments

@root9464
Copy link

Question Description

package main

import (
	"log"

	"github.com/gofiber/fiber/v3"
	"github.com/gofiber/fiber/v3/middleware/logger"
)

func main() {
	app := fiber.New()
	app.Use(logger.New())
	app.Get("/", func(Ctx fiber.Ctx) error {
		Ctx.BodyParser(struct{}{})
		return Ctx.SendString("Hello, World!")
	})
	log.Fatal(app.Listen(":3000"))
}

Ctx.BodyParser undefined (type fiber.Ctx has no field or method BodyParser)compilerMissingFieldOrMethod

Code Snippet (optional)

package main

import "github.com/gofiber/fiber/v3"
import "log"

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

  // An example to describe the question

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

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 Apr 13, 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

@VolkerLieber
Copy link

VolkerLieber commented Apr 13, 2024

It looks like it should be done via Ctx.Bind().Body
Source documentation:

  • Bind: You can bind body, cookie, headers etc. into the map, map slice, struct easily by using Binding method. It gives custom binding support, detailed binding options and more. Replacement of: BodyParser, ParamsParser, GetReqHeaders, GetRespHeaders, AllParams, QueryParser, ReqHeaderParser
  • Body: Body binds the request body into the struct, map[string]string and map[string][]string. It supports decoding the following content types based on the Content-Type header: application/json, application/xml, application/x-www-form-urlencoded, multipart/form-data If none of the content types above are matched, it'll take a look custom binders by checking the MIMETypes() method of custom binder. If there're no custom binder for m艧me type of body, it will return a ErrUnprocessableEntity error.

This change is currently not reflected in the official documentation

@gaby
Copy link
Member

gaby commented Apr 13, 2024

@VolkerLieber That's correct, the updates to the documentation are "Work In Progress".

@gaby gaby added the v3 label Apr 13, 2024
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