Plumber returning multiple images in one response?

We have created a plumber api that needs to return 5 images in one response. The processing to create all these images is in same function, but we get error informing us we cant pass more than one image in the response. Is there a decent and efficient way around this?

libraries()

#* @apiTitle x
#* @apiDescription y
#* @param a
#* @param b
#* @get /Get
#* @serializer png

function(a = "", b ="", res, req) {

str_ab<-paste0(as.character(a),"_",as.character(b))

#a only
#code to create image here
b_image<-define

#b only
#code to create image here
b_image<-define

return(a_image, b_image)

}

I do not think we have a multipart serializer yet. You would have to implement it.

What you can do is add your files to a single zip and return that.

Ok thanks!! Ill try the zip method, but might not be a goer as team taking these might not be able to deal with them, but ill see!

As an aside, for some reason it is now not even returning one image! Ive just included the file url in the return() statement. Do i need to do anything else?!

I do not see any url in the provided code sample.

Its ok, simply had to change the serializer to;

#* @serializer contentType list(type='image/png')

and the response to;

png_path <- paste0("~/dir",str_unique,"_","image.png")

# Check if the file exists
if (!file.exists(png_path)) {
  return(status = 404, body = "PNG file not found")
}

# Read the PNG file
img <-readBin(png_path,'raw')

return(img)
1 Like

I like the intent, but this idea is not possible. As @meztez stated, the images must be bundled.

Another way to bundle the images would be to return html that then requests multiple (individually requested) images from the plumber server.

Ex:

get /img_html returns:

<div>
  <img src="/img_a" />
  <img src="/img_b" />
  <img src="/img_c" />
</div>

The html could be placed directly into your DOM which would then request the images individually.

And the routes /img_a, /img_b, and /img_c return single images that are serialized using @serializer png.

(This approach could also be done with a JSON object containing the image path, alt, and any other information you may find useful.)

THanks so much, i will have a play.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.