前言
当go-micro版本是v4时,使用curl、postman等工具调用grpc gateway http接口时,会出现如下错误:
malformed header: missing HTTP content-type
解决方法
找到代码中,go-micro service创建处,添加"micro.Server(grpcsvr.NewServer())",即可。
完整示例请参考:https://github.com/go-micro/plugins/blob/main/v4/server/grpc/grpc_test.go#L237
下面为简单示例:
import (
"context"
"errors"
"net"
"sync"
"testing"
"time"
grpcsvr "github.com/go-micro/plugins/v4/server/grpc"
"go-micro.dev/v4"
"go-micro.dev/v4/client"
"go-micro.dev/v4/debug/handler"
proto "go-micro.dev/v4/debug/proto"
"go-micro.dev/v4/registry"
"go-micro.dev/v4/util/test"
)
func createService() micro.Service {
// create service
srv := micro.NewService(
micro.Name(name),
micro.Context(ctx),
micro.Registry(r),
// 新增加该行
micro.Server(grpcsvr.NewServer()),
micro.Address(net.JoinHostPort(IPv4, Port)),
micro.Metadata(map[string]string{
"ipv6": net.JoinHostPort(IPv6, Port),
}),
micro.AfterStart(func() error {
wg.Done()
return nil
}),
micro.AfterStop(func() error {
wg.Done()
return nil
}),
)
return srv
}
参考:
1.malformed header: missing HTTP content-type - bytemeta
2.Customizing your gateway | gRPC-Gateway (grpc-ecosystem.github.io) (aipdoc)