[Java][RestAssured] How to save application/octet-stream response as jpg image?

9.1k views Asked by At

I want to download images from website using RestAssured. My code:

Response response = given()
            .log().all()
            .cookie(cookie)
            .get(format(image_endpoint, "46581657"));

Endpoint returns status code 200 and image in "application/octet-stream" type. How can I save it as jpg file?

I have tried:

String bin = getImageResponse()
            .prettyPrint();
    saveFile("foto.jpg", bin); //this method only save string to file

But I cannot open jpg file. How can I save it?

1

There are 1 answers

0
Bedla On

MIME type application/octet-stream consists of bytes encoded in Base64. Use Base64 class for decoding.

private void saveFile(String path, String octetStream) throws IOException{
    byte[] bytes = Base64.getDecoder().decode(octetStream);

    try(FileOutputStream fos = new FileOutputStream(path)){
        fos.write(bytes);
    }
}

I know nothing about RestAssured, so maybe there is any way provided by framework.