I have a REST API using the Laravel framework and the front-end uses the Vue.js framework. It is a dashboard for an e-commerce store where you can connect to a post shipment service API to upload order shipment data and get a tracking number (with a label in PNG format). So after it receives a tracking number label from post service API it needs to show it in the front-end from the REST API response.
I have a route on the front-end shipments/:id/tracking/label which should show some shipment data and tracking label image.
I have 2 solutions for this. Which is better and why?
Send image content as base-64 encoded string and shipment data in one backend route GET shipments/{id}/tracking/label
Backend:
public function getTrackingLabel(Shipment $shipment, LabelIsraelPostService $service) { $data = [ 'label' => $service->getTrackingLabel($shipment->label_path), 'shipment' => $shipment ]; return response()->json(['shipment' => $data]); } Frontend:
<v-card> <v-card-title> Order id : {{ shipment.order_id }} </v-card-title> <v-img max-width="700" :src="`data:image/png;base64,${shipment.link}`" > </v-img> </v-card> Use 2 different backend routes GET shipments/{id} for all shipment data and another one for only image GET shipments/{id}/tracking_label which will return image with headers.
Backend:
For getting all shipment data:
public function show(Shipment $shipment) { return response()->json(['shipment' => $shipment]) } For getting shipment tracking label image:
public function getTrackingLabel(Shipment $shipment, LabelIsraelPostService $service) { return response(Storage::cloud()->get($path))->header('content-type', 'image\png') } Front-end usage:
<v-card> <v-card-title> Order id : {{ shipment.order_id }} </v-card-title> <v-img max-width="700" :src="`${BASE_API_URL}/shipments/${shipment.id}/tracking/label`"//or use axios for this request > </v-img> </v-card> This will upload the image in a browser from the API route and send another request to get all shipment data and show it on the front end.
I like solution #2 but if I will have 10 shipments for showing tracking number label, I will have 10 API calls for getting an image for each of them, and also one for getting an array of shipments data by ids. In solution #1 I can use only one API call for all shipments with data and base64 image for each of them.
What is best practice to send image as the REST API response and show it with JS front end?