Using a HttpResponse<byte[]> response, I cannot use mapError on it because it converts the byte array, like [97, 98, 99] into a literal String representation of the array, like '[97, 98, 99]', instead of abc, in getErrorBody.
To Reproduce
Steps to reproduce the behavior:
- Have an endpoint that would return an
octet/stream in case of success, but a JSON in case of failure, like a Spring REST controller would do, with the default error handling:
{
"timestamp": "2022-01-05T16:56:26.920+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Some message",
"path": "/getContent"
}
static class Content {
String error;
String message;
}
Unirest.get(url)
.asBytes()
.ifFailure((HttpResponse<byte[]> httpResponse) -> {
// this will invoke BaseResponse.getErrorBody
// which will invoke getBody() that returns the byte array
// and the byte array passed through `config.getObjectMapper().writeValue(body)`
// which returns something like Arrays.toString(byteArray)
Content error = response.mapError(Content.class);
response.getParsingError()
// no parsing error here actually, the object will be empty
.ifPresent(e -> logger.error("Unable to parse body", e));
if (error.message == null) {
// this will have the same result as Arrays.toString(body)
// not the actual string representation constructed from the byte array, like new String(body)
System.out.println(response.mapError(String.class));
}
});
Since I can't find such a public endpoint, I can offer a test that proves just the conversion to String:
String url = "https://httpbin.org/bytes/-1";
Unirest.get(url)
.asBytes()
.ifFailure((HttpResponse<byte[]> httpResponse) -> {
// we don't really know the response encoding, since it's stored in the request, doh, assuming UTF-8
String body = httpResponse.mapBody(bytes -> new String(bytes, StandardCharsets.UTF_8));
System.out.println(body);
// this should have printed the same HTML as above
System.out.println(httpResponse.mapError(String.class));
});
Expected behavior
I expected both mapError to be able to return an object instance populated with the values from the response, but since the byte array gets converted to String using Arrays.toString(..), it will fail.
Screenshots
If applicable, add screenshots to help explain your problem.
Environmental Data:
- Java Version: 8 latest
- Version 3.13.4
Using a
HttpResponse<byte[]> response, I cannot usemapErroron it because it converts the byte array, like[97, 98, 99]into a literal String representation of the array, like'[97, 98, 99]', instead ofabc, ingetErrorBody.To Reproduce
Steps to reproduce the behavior:
octet/streamin case of success, but a JSON in case of failure, like a Spring REST controller would do, with the default error handling:{ "timestamp": "2022-01-05T16:56:26.920+0000", "status": 500, "error": "Internal Server Error", "message": "Some message", "path": "/getContent" }Since I can't find such a public endpoint, I can offer a test that proves just the conversion to
String:Expected behavior
I expected both
mapErrorto be able to return an object instance populated with the values from the response, but since the byte array gets converted toStringusingArrays.toString(..), it will fail.Screenshots
If applicable, add screenshots to help explain your problem.
Environmental Data: