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

How can I set custom http headers in RPC service? #4466

Open
zebra-min opened this issue Nov 23, 2024 · 3 comments
Open

How can I set custom http headers in RPC service? #4466

zebra-min opened this issue Nov 23, 2024 · 3 comments

Comments

@zebra-min
Copy link

In our project, we used gRpc-Gateway. But I find, I can't set http header in RPC response.
What should we do? Thanks!

@kevwan
Copy link
Contributor

kevwan commented Nov 24, 2024

grpc-gateway or go-zero gateway?

@zebra-min
Copy link
Author

I'm not sure. I learn it from this doc "https://go-zero.dev/en/docs/tutorials/gateway/grpc".

gateway.go main code follow:

import (
	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/gateway"
	"github.com/zeromicro/go-zero/rest/httpx"
	"github.com/zeromicro/go-zero/zrpc"
	"google.golang.org/grpc/status"
)

func main() {
	flag.Parse()

	logx.DisableStat()

	var c config.Config
	conf.MustLoad(*configFile, &c)
	gw := gateway.MustNewServer(c.Gateway)
	sessionRpcClient := sessionservice.NewSessionService(zrpc.MustNewClient(c.Session))

	gw.Use(middleware.SessionMiddleware(sessionRpcClient))
	gw.Use(middleware.AuthMiddleware)

	defer gw.Stop()

	// 设置错误处理
	httpx.SetErrorHandlerCtx(grpcErrorHandlerCtx)
	fmt.Printf("Starting rpc server at %d...\n", c.Gateway.Port)
	gw.Start()
}

@kesonan
Copy link
Collaborator

kesonan commented Nov 25, 2024

It's need to pull a new request to append the http headers into context metadata.

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

No branches or pull requests

3 participants