Custom response's headers are missing from transformResponse headers argument ($resource)
Asked Answered
O

1

8

I've defined an $resource to an API endpoint that returns a response containing several headers but in the transformResponse config function most of headers are missing from the headersGetter function argument.

How can I fix it?

Response Header of the API

HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
x-frame-options: DENY
Access-Control-Allow-Origin: http://localhost:9000
access-control-allow-methods: POST, PUT, GET, DELETE, OPTIONS
access-control-allow-headers: Content-Type
Access-Control-Allow-Credentials: true
Content-Disposition: attachment; filename="testCall.pcap"
FileName: testCall.pcap
Content-Type: application/pcap

transformResponse's headers

Pragma: no-cache
Content-Type: application/pcap
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Expires: 0

app.factory("MyService", function ($resource, ENV, _) {
    return {
        testCall: $resource(ENV.apiEndpoint + "/test-call", {}, {
            launch: {
                method: 'POST',
                isArray: false,
                headers:{'Accept':'application/octet-stream'},
                responseType: 'blob',
                cache: false,
                transformResponse: function(data, headers){
                    var filename = headers('Content-Disposition'); //headers('FileName')
                    var contentType = headers('Content-Type');

                    var file = new Blob([data], {
                        type: contentType
                    });

                    var fileURL = URL.createObjectURL(file);
                    var a         = document.createElement('a');
                    a.href        = fileURL;
                    a.target      = '_blank';
                    a.download    = filename;
                    document.body.appendChild(a);
                    a.click();
                }
            }
        }),
        searchOptions: $resource(ENV.apiEndpoint + "//search-options")
    };
});
Orography answered 21/10, 2015 at 10:48 Comment(0)
E
13

Assume you are making CORS calls, response headers are not all exposed. Server-side needs to add response header "Access-Control-Expose-Headers" in CORS filter.

e.g. To read a custom response header named "X-MY-HEADER1" and "X-MY-HEADER2" with CORS call, server add header

Access-Control-Expose-Headers: "X-MY-HEADER1, X-MY-HEADER2"

See the answer by @nancoder at https://mcmap.net/q/1019915/-how-to-read-response-headers-with-resource

Evangelista answered 23/10, 2015 at 9:39 Comment(3)
Thks for your response. Yes, it's a CORS call on a sub-domain but the above response headers is the one received by the Angular server (I've gotten them with Firebug). So for me I think that Angular framework cleans some of them before the transformResponse...Orography
+1 Man, I was willing to know exact this answer like crazy, I came across the same problem when tried to read the Location header after a create a resource in a cross domain RESTFull Api and grab the ID from the header Location, by adding the Header on the server did the trick! Tks for the great answer.Perlis
finally, i was able to get my custom token. thanks Joe and Adriano.Larynx

© 2022 - 2024 — McMap. All rights reserved.