Reputation: 1
Apache Camel: How to decode responseBody when receiving a 400 error?
I am using Apache Camel to send HTTP requests. When the request is successful, the response body is correctly represented. However, if the server returns a 400 Bad Request, the responseBody (from HttpOperationFailedException) appears as [B@3f6751a, indicating it is a byte[].
Here’s the relevant part of my Camel route:
.toD("${exchangeProperty.url}?bridgeEndpoint=true&charset=UTF-8")
.to("log:DEBUG?showBody=true&showHeaders=true")
.onException(HttpOperationFailedException.class)
.log("Exception: ${exception.responseBody}")
When there is no error, the response body is properly formatted.
Logs:
CamelServletContextPath=/postRestApi, Connection=keep-alive, Content-Length=43,
Content-Type=application/json; charset=utf-8, Date=Tue, 18 Feb 2025 13:55:21 GMT,
ETag=W/"2b-hGShxOkieaAVDloBubJVM+h58D8", Server=openresty,
Strict-Transport-Security=max-age=63072000;includeSubDomains; preload, X-Powered-By=Express},
BodyType: byte[], Body: [B@1d6f2a32
How can I properly decode responseBody when an error occurs?
The issue is that when I try to decode responseBody into a string, I get a type mismatch errorString
decodedResponse = new String(responseBody, StandardCharsets.UTF_8);
constructor java.lang.String.String(byte[],int) is not applicable (argument mismatch; java.lang.String cannot be converted to byte[])
[B@3f6751a is a String.
Upvotes: 0
Views: 21