Angular 4 new HttpClient content-disposition header
Asked Answered
V

2

22

I am trying to get a file (array buffer) from my backend using the new HttpClient.

My code is pretty much like the following:

this.http.post(url, body, {responseType: 'blob', observe: 'response'})
  .subscribe(resp => {
    console.log(resp.headers.get('content-disposition'));
  });

The repsonse in my browser console is null

When I take a look at the network tab, then I see that content-disposition has a value like attachment; filename="whatever"

If I try to get content-type instead, then I receive something.

Have you experienced something similar or can you tell me what I have to do else? Many thanks

Verwoerd answered 7/11, 2017 at 14:22 Comment(1)
there is a relation to #46389988 but I could not solve it yetVerwoerd
T
59

I had the same Issue and found this https://mcmap.net/q/587213/-with-angular-39-s-new-httpclient-how-can-i-get-all-headers-when-subscribing-to-the-events

It worked for me after adding 'Access-Control-Expose-Headers': 'Content-Disposition' in my backend response header.

Topminnow answered 8/11, 2017 at 7:47 Comment(4)
That was exactly what I searched for. Solved my problem. Thank you =)Verwoerd
Thanks! This was also useful: #38898264Claxton
Thanks great find, the issue is so misleading because in browser you see the content-disposition but angular does not see it until you tell the server specifically to expose the specific header.Fidelis
I didn't need 'Access-Control-Expose-Headers': 'Content-Disposition' in Angular 12 (anymore?!)Blowpipe
R
0

if your backend is using SpringSecurity you need add ExposeHeader to configuration source:

@Bean
protected CorsConfigurationSource corsConfigurationSource() {
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.addExposedHeader("Content-Disposition");
    return source;
}
Reforest answered 22/10, 2020 at 6:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.