Download file is no longer works with SmartAction (with POST http call)

Expected behavior

As described in the documentation, it’s possible to download a file on a smart action response using the POST Http verb :
https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions#downloading-a-file

Actual behavior

Try to create a smart action as described in the documentation:
https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions#downloading-a-file

It is important to use a Smart Action with POST and not GET.
When you call your smart action, the server returns an HTTP response containing a valid response with the file attached.
But, the FrontAdmin interface tries to parse into JSON the body in the HTTP response. Obviously, is not parsable, and the interface display an error message.
There is a “workaround”, if you switch your Smart action in GET. It works, but you don’t have the input parameters …

Failure Logs

Please include any relevant log snippets, if necessary.

Context

Please provide any relevant information about your setup.

  • Package Version: tested on 6.6.2 and 5.7.0
  • Express Version: 4.17.1
  • Sequelize Version: 5.22.3
  • Database Dialect: Postgres
  • Database Version: 11
  • Project Name: Swan Back-Office

Hi @Mathieu_Breton :wave: Thank you for your feedback!

I just tried this, with a User model, it seems it works (using POST):

// In file `forest/user.js`, don't forget "download true".
 { name: 'Generate invoice', download: true },

// In a `routes/user.js`
router.post(
  '/actions/generate-invoice',
  permissionMiddlewareCreator.smartAction(),
  (req, res, next) => {
    let options = {
      root: __dirname + '/../public/',
      dotfiles: 'deny',
      headers: {
        'Access-Control-Expose-Headers': 'Content-Disposition',
        'Content-Disposition': 'attachment; filename="favicon.png"'
      }
    };

    let fileName = 'favicon.png';
    res.sendFile(fileName, options, (error) => {
      if (error) { next(error); }
    });
  });

:point_right: Could you share a simplified version of the code in your smart action definition and route? Maybe there is something missing.

2 Likes

Hi guys,

Sorry for the delay, I figured out what it was my problem.
It appear the download feature doesn’t work when the server responds with the HTTP code 201.