Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get "Content-Disposition" Header of a request with Axios

Tags:

cors

header

axios

I'm trying to get the 'Content-Disposition' Header of a request from api call by axios like this:

axios.get('Group/GetGroupObjectives', {
    params: { periodId, isPreliminary },
    responseType: 'arraybuffer',
  })
      .then((response) => {
        if (response) {
          response.request.getResponseHeader('Content-Disposition');
        } else {
          dispatch(docDownloadFailed());
        }
      })

When i get the header throws this error "Refused to get unsafe header "Content-Disposition""

This problem is caused by Cors in the api, but i get all the headers needed for get the header correctly in the response header:

Access-Control-Allow-Origin:*
Access-Control-Expose-Headers:*
Access-Control-Request-Headers:*
Cache-Control:no-cache
Content-Disposition:attachment; filename="sample.xlsx"
Content-Length:7965
Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Date:Fri, 26 Jan 2018 14:35:38 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/10.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?RDpcVGVhbV9Tb2Zhc2FcRXh0cmFuZXRcRXh

How do I get the response header correctly with axios call?

like image 756
Simon Restrepo Avatar asked Jan 26 '18 14:01

Simon Restrepo


1 Answers

If you are using WEB API from .NET, you can set these headers in the web.config

   <customHeaders>
             <add name="Access-Control-Expose-Headers" value="Content-Disposition,X-Suggested-Filename"/>
      </customHeaders>

Grtz

like image 200
Ben Croughs Avatar answered Oct 19 '22 23:10

Ben Croughs