isomorphic-fetch icon indicating copy to clipboard operation
isomorphic-fetch copied to clipboard

Can not read content-disposition from resposne header

Open pkumar84 opened this issue 9 years ago • 18 comments

I am creating download url at frontend side and server send me data in response body and content-type, content-disposition in response header. i am able to read response.headers.get('content-type') but not response.headers.get('content-disposition'), is response.headers.get('content-disposition') does not supported ?

pkumar84 avatar Jan 20 '16 12:01 pkumar84

We are experiencing a similar issue. The response object coming back from fetch is missing headers besides content-type. In our case we are looking for X-Vcap-Request-Id.

we're able to see a number of headers in the network tab of chrome's devtools, but the only one that is available via the headers through this lib is response.headers.get('content-type')

cmloegcmluin avatar Mar 08 '16 22:03 cmloegcmluin

Any news on this? I have the very same problem and I am getting nervous. ;)

malagant avatar Sep 21 '16 13:09 malagant

I'm having a similar problem: the only headers i can see are: 'cache-control', 'content-type' and 'expires'. I can see more headers in chrome devtools, too. And i really need to read those hawk headers...

tw-360vier avatar Sep 27 '16 11:09 tw-360vier

Since this is an issue with the browser side implementation (https://github.com/github/fetch) of this package, doesn't this issue belong there? Also, i think my problems are probably caused by some missing CORS settings: https://github.com/github/fetch/issues/399

tw-360vier avatar Oct 07 '16 13:10 tw-360vier

Me too. Seeing the Content-Disposition header in Chrome devtools but response.headers.get('content-disposition') is null :s

jch254 avatar Oct 18 '16 08:10 jch254

At least I don't believe that this is an issue of isomorpic-fetch oder fetch either. We use a rails api backend and solved the issue with this in a cors initializer:

# Be sure to restart your server when you modify this file.

# Avoid CORS issues when API is called from the frontend app.
# Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests.

# Read more: https://github.com/cyu/rack-cors

if Rails.env == 'development'
    Rails.application.config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins 'localhost:10020'

        resource '*',
          headers: :any,
          methods: [:get, :post, :put, :patch, :delete, :options, :head],
          expose: [
            'X-Total', 
            'X-Total-Pages', 
            'X-Page', 
            'X-Per-Page', 
            'X-Next-Page', 
            'X-Prev-Page',
            'X-Offset',
            'X-Rdb-Lang'
          ]
      end
    end
end

As you can see, we are exposing explicitly needed headers. And so should you with content-disposition. Although this is a rails example. You should be able to expose the headers in JavaScript also.

Hope this helps someone.

malagant avatar Oct 18 '16 08:10 malagant

@malagant you're right! Even if the headers show in Chrome devtools they're not necessarily included in a CORS response. I explicitly exposed the Content-Disposition header server side and all worked. Cheers cheers!

jch254 avatar Oct 18 '16 09:10 jch254

Using isomorphic-fetch in Node/Express to request images from third party API (thus can't control certain CORs settings). Is there a way to get a full set of headers from Fetch/third party API? Using Node Request exposes all headers, Fetch does not.

Only headers returned in Fetch are: cache-control expires content-type content-length

No resolution to this yet?! :(

JoshMcCullough avatar Mar 07 '17 19:03 JoshMcCullough

I have same issue, can see 'X-Total-Count' in Chrome devtool, but there is no such key in fetch's response headers.

derekgray23 avatar Mar 22 '17 07:03 derekgray23

Add the "Access-Control-Expose-Headers" header to response from server. To read from javaScript "content-disposition" header add to response on server this Access-Control-Expose-Headers:Content-Disposition. Then this code will work: response.headers.get('content-disposition')

Cherepanov6 avatar Dec 22 '17 14:12 Cherepanov6

In C# you could do so by writing: result.Content.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition"); At least it worked for me finally.

wdamianw avatar Jun 25 '18 10:06 wdamianw

Add the "Access-Control-Expose-Headers" header to response from server. To read from javaScript "content-disposition" header add to response on server this Access-Control-Expose-Headers:Content-Disposition. Then this code will work: response.headers.get('content-disposition')

thank you, You saved my day.

huync94 avatar Apr 09 '20 04:04 huync94

I did set the response header mentioned by @Cherepanov6 in Express NodeJS with the below code.

res.setHeader('Access-Control-Expose-Headers','Content-Disposition');

Thanks @Cherepanov6 !

av1v3k avatar Feb 27 '21 13:02 av1v3k

In C# you could do so by writing: result.Content.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition"); At least it worked for me finally.

Step 1: first just check Content-Disposition in header response step 2 : .WithExposedHeaders("Content-Disposition") as this in cors access or startup.cs class in a case of Asp.net core Step 3 : ( blob).name = this.getResponseHeader('content-disposition').match(/filename=(.+);/)[1].replace(/(^")|("$)/g, '');

dhkha2749 avatar Apr 29 '21 08:04 dhkha2749

(file gets saved in binary format in the browser. the filename is in the client's Network/header/Content-Disposition, we need to fetch the filename)

In Server-side code: node js code- response.setHeader('Access-Control-Expose-Headers','Content-Disposition'); response.download(outputpath,fileName);

In client-side code: 1)appComponent.ts file import { HttpHeaders } from '@angular/common/http'; this.reportscomponentservice.getReportsDownload(this.myArr).subscribe((event: any) => { var contentDispositionData= event.headers.get('content-disposition'); let filename = contentDispositionData.split(";")[1].split("=")[1].split('"')[1].trim() saveAs(event.body, filename); }); 2) service.ts file import { HttpClient, HttpResponse } from '@angular/common/http'; getReportsDownload(myArr): Observable<HttpResponse<Blob>> { console.log('Service Page', myArr); return this.http.post(PowerSimEndPoints.GET_DOWNLOAD_DATA.PROD, myArr, { observe: 'response', responseType: 'blob' }); }

kul-swat avatar Jul 30 '21 15:07 kul-swat

If you are using django, you would add this to settings file: CORS_EXPOSE_HEADERS =[ 'content-disposition',
]

sharan5am avatar Oct 29 '21 18:10 sharan5am

Does anybody know how to expose "Content-Disposition" header in Next-js. I tried editing the next.config.js file according to this article but with no luck.

/** @type {import('next').NextConfig} */
module.exports = {
  reactStrictMode: true,
  typescript: {
    ignoreBuildErrors: true,
  },
  async headers() {
    const securityHeaders = [
      {
        key: 'X-Content-Type-Options',
        value: 'nosniff'
      },
      // here
      { key: "Access-Control-Expose-Headers", value: "Content-Disposition" },
    ]
    return [
      {
        source: '/:path*', // req path
        headers: securityHeaders
      }
    ]
  }
}

f-starace avatar Feb 14 '22 14:02 f-starace