[英]GRPC over HTTPS proper client initialization in Go
我正在从我的代码连接某些通过 GPRC 提供 API 的第 3 方软件(尤其是 chirpstack v4)。 在本地部署时,它通过 HTTP 回答,我得到了很好的响应。
但是在集群中,我们使用HTTPS
部署了同一台服务器(使用letsencrypt证书,而不是私有的)所以我试图添加相应的传输层安全设置,但令我惊讶的是我得到了
rpc error: code = Internal desc = unexpected HTTP status code received from server: 400 (Bad Request); malformed header: missing HTTP content-type
我尝试使用content-type: application/grpc
将SetHeader
添加到上下文中,但这不会改变任何东西,所以我不确定它是否真的是关于 header (而且它适用于纯 HTTP)。 所以我想知道,也许有人可以指出我在初始化传输层安全性时的一些错误?
// this is used with plain HTTP
//opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
// this for case of working via HTTPS
opts := []grpc.DialOption{grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{}))}
// also tried NewClientTLSFromCert(x509.SystemCertPool()) - the same
serverAddr := "our-public-address:443"
ctx := context.Background()
conn, err := grpc.Dial(serverAddr, opts...)
if err != nil {
println("Dial error:", err.Error())
return
}
cli := api.NewInternalServiceClient(conn)
req := &api.LoginRequest{Email: "admin", Password: "admin"}
resp, err := cli.Login(ctx, req) // error arises here
if err != nil {
println("login failed", err.Error())
}
提前感谢您的提示和建议!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.