omidh
omidh

Reputation: 2822

Hapi: How to send image

I have a jpeg image binary, How can I use Hapi to show the image? My code only shows garbage to the end user of the API.

hapiServer.route({
  method: 'GET',
  path:'/users/{userId}/photo',
  handler: async function (request, reply) {
    const userId = parseInt(encodeURIComponent(request.params.userId));


    const photo = getImageBinary(userId);    
    reply(photo);
  }
});

Upvotes: 2

Views: 2596

Answers (1)

aring
aring

Reputation: 3602

Assuming your picture binary data is png:

hapiServer.route({
  method: 'GET',
  path:'/users/{userId}/photo',
  handler: async function (request, reply) {
    const userId = parseInt(encodeURIComponent(request.params.userId));

    const photo = getImageBinary(userId);    
    reply(photo).header('Content-Disposition','inline').header('Content-type','image/png');
  }
});

Upvotes: 1

Related Questions