Own plugin: problem with 'request' and 'axios'

System Information
  • Strapi Version: 3.3.3
  • Node Version: 14.14

Hi community,

I come here because I have a problem with the implementation of a data export plugin.

I want an export in CSV format of the data of a collection.

At the beginning, I used a strapi-helper-plugin request coupled with a global middleware (with condition on the header type ‘text/csv’.

Middleware:

  return {
    initialize() {
      strapi.app.use(async (ctx, next) => {
        await next();
        if (ctx.header.accept === 'text/csv') {
          ctx.type = 'text/csv';
          ctx.send(strapi.plugins['import-export-csv'].services['import-export-csv'].parseCsv(ctx.body));
        }
      });
    },
  };
};

But request blocks access to the response if I change the content type. The response is OK (status 200, and I have my data in my inspector), but I never have access to it in my React code.

So I wanted to use axios directly. It’s ok, I can download my CSV, but I have to pass a Bearer authorization in the header, and I don’t know how to access this token in the React code of my plugin :(…

getCsv = async () => {
    const { selectedContentType } = this.state;
    const url = ${strapi.backendURL}/${selectedContentType.name};
    const response = await axios({
      method: 'get',
      url,
      headers: {
        Accept: 'text/csv',
        Authorization: 'Bearer ???'
      },
    });
    generateCSVLink(response.data, selectedContentType.name);
  };

Does anyone have a solution, either to unblock the use of request, or to find out how to get the user’s token in the Front React of my plugin, it would help me a lot.

Thanks for your help!

EDIT:
Ok,I solved part of the problem by using auth from strapi-helper-plugin to get my token from auth.getToken() for my axios request.
But i’m curious to have discussion about the request of strapi-helper-plugin to get a text/csv content-type response…

Hello, how did you decide what to do next? when i use this token i get this
Снимок экрана 2022-07-21 174231