diff --git a/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcUtils.java b/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcUtils.java index bd7c60c56b9..778c95aee35 100644 --- a/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/remote/client/grpc/GrpcUtils.java @@ -32,6 +32,8 @@ import com.google.protobuf.UnsafeByteOperations; import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.Map; /** * gRPC utils, use to parse request and response. @@ -59,8 +61,7 @@ public static Payload convert(Request request, RequestMeta meta) { payloadBuilder.setMetadata(metaBuilder.build()); // request body . - request.clearHeaders(); - byte[] jsonBytes = JacksonUtils.toJsonBytes(request); + byte[] jsonBytes = convertRequestToByte(request); return payloadBuilder .setBody(Any.newBuilder().setValue(UnsafeByteOperations.unsafeWrap(jsonBytes))) .build(); @@ -77,11 +78,11 @@ public static Payload convert(Request request) { Metadata newMeta = Metadata.newBuilder().setType(request.getClass().getSimpleName()) .setClientIp(NetUtils.localIP()).putAllHeaders(request.getHeaders()).build(); - request.clearHeaders(); - byte[] jsonBytes = JacksonUtils.toJsonBytes(request); + + byte[] jsonBytes = convertRequestToByte(request); Payload.Builder builder = Payload.newBuilder(); - + return builder .setBody(Any.newBuilder().setValue(UnsafeByteOperations.unsafeWrap(jsonBytes))) .setMetadata(newMeta).build(); @@ -103,6 +104,14 @@ public static Payload convert(Response response) { .setMetadata(metaBuilder.build()).build(); } + private static byte[] convertRequestToByte(Request request) { + Map requestHeaders = new HashMap<>(request.getHeaders()); + request.clearHeaders(); + byte[] jsonBytes = JacksonUtils.toJsonBytes(request); + request.putAllHeader(requestHeaders); + return jsonBytes; + } + /** * parse payload to request/response model. *