17

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?

1
  • 4
    The value of Access-Control-Expose-Headers response header can’t be the * wildcard. Instead it has to explicitly include Content-Disposition and the names of any other response headers you want to access from your frontend JavaScript code.
    – sideshowbarker
    Jan 27, 2018 at 22:35

1 Answer 1

23

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

2
  • Also check this article if you are using VueJs in combination with AXIOS, has a lot of useful info, certrainly if you want a blob from an AXIS call use {responseType: 'blob'}). stackoverflow.com/questions/41938718/… Mar 26, 2018 at 7:26
  • @SimonRestrepo can you share your solutions as one of the answer? other might need it. thanks!
    – jedi
    May 15, 2019 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.