I have a client that consumes an input stream from a service, like this:
@Client(value = 'http://localhost:8080/')
interface StreamClient {
@Get(uri = '/data', processes = MediaType.APPLICATION_OCTET_STREAM)
@Header("Accept: application/octet-stream")
Flowable<byte[]> retrieveFile()
}
and when using it like this (Groovy):
void getContent(OutputStream outputStream) {
client.retrieveFile().blockingSubscribe({ byte[] bytes ->
outputStream.write(bytes)
}, { Throwable error ->
error.printStackTrace()
})
}
it retrieves the stream just fine without reading the entire byte[] array into memory (exactly as I want it), but if the endpoint returns a 404, I just get an empty byte array, not an error as I would expect.
What am I doing wrong?
The Micronaut http client returns the declared return type (in your case
byte[]) for status 2xx and 404. In the case of 404 you usually getnullfor objects and in your case with a primitive array it seems to be an empty array.In order to get more control I recommend to return an
HttpResponse<byte[]>. With this type you get access to the http status code.Further readings about error responses and Micronaut http client.