-1

If anyone using Axios ( i faced it in Nuxtjs ) are not able to get any response headers except the content-type then the reason behind it is that by default CORS ( server ) doesnt allow the exposure of headers except the content-type due to security reasons. to override the same please follow below mentioned steps.

Solution is to allow the exposure of headers from backend inside CORS by doing this :

On Server Side :-

Insert a cors middleware in your Nodejs Express application or server if you built your API using Nodejs and Express, and set the headers like below:

app.use(
    cors({
        origin: "*",
        allowedHeaders: ["*"],
        exposedHeaders: ["*"]    
    })
);

Here (*) will allow exposure of all headers if you want any particular header to be exposed to client side just mention its name as string inside the exposed headers.

On Client Side :-

const contentDisposition = response.headers['content-disposition'];

or if you want to get filename using it just write ,

const filename = response. Headers['content-disposition']
        .split(';')
        .find((n) => n.includes('filename='))
        .replace('filename=', '')
        .trim()

0 Answers0