I want to read/write POJO from/to protobuf directly to support large payload.
I cannot use ProtobufHttpMessageConverter which only support Message object.
So I write a HttpMessageConverter to archive that, and meet the following issue.
SpringEncoder will not use charset for protobuf,
but it force the the type of message converter is ProtobufHttpMessageConverter.
The charset of request body will be set to StandardCharsets.UTF_8 when I use other HttpMessageConverter for protobuf MediaType.
SpringEncoder.java ```spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/SpringEncoder.java#L128 // do not use charset for binary data and protobuf Charset charset; if (messageConverter instanceof ByteArrayHttpMessageConverter) { charset = null; } else if (messageConverter instanceof ProtobufHttpMessageConverter <------------- && ProtobufHttpMessageConverter.PROTOBUF.isCompatibleWith( outputMessage.getHeaders().getContentType())) { charset = null; } else { charset = StandardCharsets.UTF_8; } request.body(Request.Body.encoded( outputMessage.getOutputStream().toByteArray(), charset)); return;
When the charset of request body is set,
feign will construct a `String` by decoding the byte array with the specified charset.
**The request body is corrupt now**, because it is binary data but not a UTF_8 string.
[ApacheHttpClient.java](https://github.com/OpenFeign/feign/blob/v8.18.0/httpclient/src/main/java/feign/httpclient/ApacheHttpClient.java#L139)
// request body if (request.body() != null) { HttpEntity entity = null; if (request.charset() != null) { ContentType contentType = getContentType(request); String content = new String(request.body(), request.charset()); <------------- entity = new StringEntity(content, contentType); } else { entity = new ByteArrayEntity(request.body()); }
requestBuilder.setEntity(entity); } else { requestBuilder.setEntity(new ByteArrayEntity(new byte[0])); } ```
I think we can solve this issue by removing the the message converter type check, messageConverter instanceof ProtobufHttpMessageConverter.
The media type check for protobuf is enough.
Comment From: spencergibb
We currently don't use media type at all for this check. If you've written a custom Converter, I'd suggest writing a custom Encoder that delegates to SpringEncoder and does what you need.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.