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 get added header by proxy #947

Closed
jney opened this issue Oct 21, 2020 · 5 comments
Closed

🐛 Can't get added header by proxy #947

jney opened this issue Oct 21, 2020 · 5 comments

Comments

@jney
Copy link
Contributor

jney commented Oct 21, 2020

Fiber version

v2.1.0

Issue description

Can't get the added header by proxy in targeted server.
Look at the following code, I can't get "test" header.

Code snippet

	app := fiber.New()

	app.Use(compress.New())
	app.Use(cors.New(cors.Config{AllowCredentials: true}))

	app.Use(proxy.Balancer(proxy.Config{
		Servers: []string{"http://localhost:9090"},
		ModifyRequest: func(c *fiber.Ctx) error {
			c.Set("test", "ok")
			return nil
		},
	}))

	app.Listen(":8888")

in targeted server

c.Get("test") // returns ""
@welcome
Copy link

welcome bot commented Oct 21, 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 Oct 21, 2020

Quick question, are you testing this with CORS requests specifically?

@jney
Copy link
Contributor Author

jney commented Oct 21, 2020

Hi @Fenny , I tested without it too, same result. I should have removed it from example to avoid confusion.

You can try it with this very minimal code:

proxy:

package main

import (
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/proxy"
)

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

	app.Use(proxy.Balancer(proxy.Config{
		Servers: []string{
			"http://localhost:8001",
		},
		ModifyRequest: func(c *fiber.Ctx) error {
			c.Set("hello", "world")
			return nil
		},
	}))

	app.Listen(":8000")
}

server:

package main

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

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

	app.All("/*", func(c *fiber.Ctx) error {
		return c.SendString("hello "+c.Get("hello"))
	})

	app.Listen(":8001")
}

run both, then curl 'http://0.0.0.0:8000/': still empty header

@jney
Copy link
Contributor Author

jney commented Oct 22, 2020

I figured out. It looks like I have to use c.Request().Header.Add instead of c.Set.
Maybe is it me, or the README is confusing?

@jney jney closed this as completed Oct 22, 2020
@Fenny
Copy link
Member

Fenny commented Oct 22, 2020

I will update the example 👍

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