1

I am building application like Airbnb and when I want to view certain accommodation object I need to send some JSON data about that object from database alongside with multiple images ( Images are stored on server and their path is stored in database ). What is the proper way to accomplish this. This is what I fetch from DB:

"data": {
        "accObject": {
            "id": 21,
            "owner": 9,
            "name": "Vila Ponta",
            "address": "Josipa Broza Tita BB",
            "advance_payment": 1,
            "advance_amount": 50,
            "non_refundable_advance": 0,
            "is_approved": 0,
            "created_at": "2022-03-15T17:40:24.000Z"
        },
        "accObjectPhotos": [
            {
                "phot_id": 30,
                "photo_url": "C:\\Users\\Nikola\\Desktop\\petrovac-projekat\\backend/documents//acc_objects_photos/3_21.jpg"
            },
            {
                "phot_id": 31,
                "photo_url": "C:\\Users\\Nikola\\Desktop\\petrovac-projekat\\backend/documents//acc_objects_photos/5_25.jpg"
            },
            {
                "phot_id": 32,
                "photo_url": "C:\\Users\\Nikola\\Desktop\\petrovac-projekat\\backend/documents//acc_objects_photos/5_26.jpg"
            },
            {
                "phot_id": 33,
                "photo_url": "C:\\Users\\Nikola\\Desktop\\petrovac-projekat\\backend/documents//acc_objects_photos/91_1.jpg"
            }

Is it bad practice if I transform all images in JSON to base64 string and then send that inside JSON object.

1
  • base64 encoding will inflate the size of the image to 133% of its original size (in bytes), so that part is bad practice, yes. Commented Mar 16, 2022 at 15:58

1 Answer 1

1

I think sending the links is best practice unless the links aren't publicly available. If thats the case, I can think of a couple other options:

  1. Like you said, base64 encode and include in the JSON
  2. Create an API that makes those images available publicly and return the links to that endpoint. (you can even add onetime auth so they can't keep downloading)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. What do you mean by sending the links ? Images are stored on server
Typically a REST API will respond with JSON that contains links to additional resources that can be fetched from the server separately in a follow-up request. e.g.: { "name": "mne_web_dev", "photo": "https://example.com/api/photos/mne_web_dev.jpg" } In this case, the JSON is the response to the primary request, and the photo would be retrieved by the client making a GET request to the photo URL provided in the JSON of the response to the first request.
The link: "photo_url": "C:\\Users...... This link doesn't look like one a client can follow. So, instead, you could make a new endpoint that requests an image and your api delivers it from a locally available location like C:\\.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.