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

🤗 Custom response ? #747

Closed
s1ntaxe770r opened this issue Sep 3, 2020 · 4 comments
Closed

🤗 Custom response ? #747

s1ntaxe770r opened this issue Sep 3, 2020 · 4 comments

Comments

@s1ntaxe770r
Copy link

Question description
im trying to return a custom response when a user completes an upload but i seems to be missing something because its not working , neither of the response seem to be working.

Code snippet Optional

package handlers

import (
	"fmt"
	"log"
	"github.com/gofiber/fiber"
)

func Upload() fiber.Handler {
	return func(c *fiber.Ctx) {
		file, err := c.FormFile("file")
		if err != nil {
			log.Printf("err from logger:>  %s", err)
			c.Send(fiber.Map{
				"message": err,
			})
		}
		c.SaveFile(file, fmt.Sprintf("./%s", file.Filename))
		c.Send(fiber.Map{
			"status": "success",
		})

	}
}
@welcome
Copy link

welcome bot commented Sep 3, 2020

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

@Fenny
Copy link
Member

Fenny commented Sep 3, 2020

Also make sure that your BodyLimit is set to the max size you want to upload, it defaults to 4MB (4 * 1024 * 1024)

app := fiber.New(&fiber.Settings{
    BodyLimit: 12 * 1024 * 1024, // 12 MB
})
func Upload() fiber.Handler {
	return func(c *fiber.Ctx) {
		file, err := c.FormFile("file")
		if err != nil {
			log.Printf("err from logger:>  %s", err)
			c.JSON(fiber.Map{
				"message": err,
			})
		}
		// Pass error to errorhandler
		if err := c.SaveFile(file, fmt.Sprintf("./%s", file.Filename)); err != nil {
			c.Next(err)
			return
		}
		// Format JSON
		c.JSON(fiber.Map{
			"status": "success",
		})

	}
}

@s1ntaxe770r
Copy link
Author

s1ntaxe770r commented Sep 3, 2020

Thank you so much it works now. Why do you have to return after c.Next(err)

@Fenny
Copy link
Member

Fenny commented Sep 3, 2020

Else it would continue to modify the response, when you return from the handler it will flush the response to the client.
In the upcoming version v1.15.x it requires you to return an error.

// v1.14.5
func Upload() fiber.Handler {
	return func(c *fiber.Ctx) {
		file, err := c.FormFile("file")
		if err != nil {
			log.Printf("err from logger:>  %s", err)
			c.JSON(fiber.Map{
				"message": err,
			})
		}
		if err := c.SaveFile(file, fmt.Sprintf("./%s", file.Filename)); err != nil {
			c.Next(err)
			return
		}
		c.JSON(fiber.Map{
			"status": "success",
		})
	}
}
}
// v1.15.0 ( 15 September 2020 )
func Upload() fiber.Handler {
	return func(c *fiber.Ctx) error {
		file, err := c.FormFile("file")
		if err != nil {
			return err // calls global error handler
		}
		if err := c.SaveFile(file, fmt.Sprintf("./%s", file.Filename)); err != nil {
			return err // calls global error handler
		}
		return c.JSON(fiber.Map{"status": "success"})
	}
}

@Fenny Fenny closed this as completed Sep 3, 2020
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

2 participants