1

I'm doing a project of mine and I'm trying to return an Json object with an image and some other data to the client, is this possible? And if its not, if I return the image as a byte array or base64 would a frontender be able to convert it back to an actual image or the conversion should be done on my end?

    {
        "author": "Uponn",
        "title": "Some title",
        "likes": 10000000,
        "file": *image here*,
        "uploadTime": null
    }
Uponn
  • 199
  • 2
  • 3
  • 15
  • Possible duplicate of [How do you put an image file in a json object?](https://stackoverflow.com/questions/34485420/how-do-you-put-an-image-file-in-a-json-object) – Golov Pavel May 24 '19 at 15:11
  • 1
    Probably encode the image bytes as base64 and put it in the json as a string. Better way would be to return a link to the image instead. – rdas May 24 '19 at 15:12
  • you can typically create images with a url i have a post that handles serving just the image via spring. You probably still want to create a custom serializer to inject either the link or the base64 string. https://stackoverflow.com/questions/40557637/how-to-return-an-image-in-spring-boot-controller-and-serve-like-a-file-system/44813266#44813266 – mavriksc May 24 '19 at 15:18
  • @rdas so by just returning the path of the file would be fine? Because that eases me since in my DB i just save the path of the files. mavriks I would actually try that, thanks. – Uponn May 24 '19 at 15:48
  • Would the the path make sense to the client? Will they be able to access it? I'm guessing no. But it depends – rdas May 24 '19 at 15:49
  • @rdas oh I though "return a link to the image instead" that link == path. The images are taken from my server so links I guess won't work. – Uponn May 24 '19 at 15:53
  • Yeah. Best way is to store the images in a place where the client can access it - like S3. And then return a link to that place. – rdas May 24 '19 at 15:54
  • I appreciate the accept, and welcome to upvote levels ;-) – GhostCat Jun 03 '19 at 13:58

1 Answers1

2

Answer is: depends.

I'm doing a project of mine and I'm trying to return an Json object with an image and some other data to the client, is this possible?

You are the one defining the API/interface of your application. If you want that this service returns a JSON object that contains that data, then yes: that is possible.

if I return the image as a byte array or base64 would a frontender be able to convert it

Sure. If you clearly specify what exactly the service is doing. So: when your backend reads the raw bytes of some image from disk, and puts these bytes (somehow encoded, maybe using base64) into a string. Sure, then any client should be able re-build the raw bytes, to then do with that information whatever the user wants to use them for.

In other words: nothing you ask for is technically impossible. The key thing for you to understand: we can't tell you your requirements. You have to identify why and how people will want to use your service(s). And then you design them in ways that support the agreed on use cases.

GhostCat
  • 137,827
  • 25
  • 176
  • 248