1

I need to find a way to change the content type of a response I'm sending from a custom remote method. It seems that by default it's application/json.

I have a remote method that returns images, so it i need somehow to change the Content-Type.

ppoliani
  • 4,792
  • 3
  • 34
  • 62

2 Answers2

5

Register remote hook and then set header on express-like res from context object. In the end call next function (if it's defined) to continue execution.

Model.afterRemote('fetch', function(ctx, instance, next) {
   ctx.res.header('Content-Type', 'image/png');
   next && next();
});
xangy
  • 1,185
  • 1
  • 8
  • 19
IvanZh
  • 2,265
  • 1
  • 18
  • 26
2

You could add the response object of express to your method signature

Model.remoteMethod(
    'yourMethod',
    {
        accepts: [
            { arg: 'id', type: 'number', required: true },
            {arg: 'res', type: 'object', 'http': {source: 'res'}}
        ],
        http: {path: '/method/:id', verb: 'get'}
    }
);

and then use this object to set the content-type

Model. yourMethod = function(id, res, cb) {
    res.type("image/png");
    cb();
}
choise
  • 24,636
  • 19
  • 75
  • 131