Help! my response Interceptor is not seeing all the available response headers!

1,205 views
Skip to first unread message

Al Joslin

unread,
Jun 23, 2015, 6:08:30 PM6/23/15
to ang...@googlegroups.com
Help!  I don’t know what I’m doing wrong here !

I can’t get my response interceptor to see all the response headers that should be available.

I don’t get it !!

I sure hope someone can point me in the right direction !!

Al;

—————>

I’m calling the server using a $resource

    var BookletData = $resource(ENV.backend+'/booklet/:id', { id: '@scriptRow.id' } );

 
—————>


I have the following response interceptor defined in my app.config()

    $provide.factory('responseInterceptor', function() {
      return {
        response: function(resp) {
          console.log('response.config.url',resp.config.url);
          console.log('response.headers.AuthToken',resp.headers('AuthToken'));
          console.log('response.headers',resp.headers());
          return resp;
        }
      };
    });

    $httpProvider.interceptors.push('responseInterceptor');


—————>


And can I see it’s output in the console so I know it’s working:

app.js:67 response.config.url http://localhost:8000/api/booklet/11
app.js:68 response.headers.AuthToken null
app.js:69 response.headers Object {content-type: "application/json", cache-control: "no-cache"}
angular.js:11699 booklet:11 was loaded


—————>


And yet the headers that were returned (based on the: Live HTTP Headers chrome extension) were much more extensive and included the AuthToken that I am looking for !!


GET http://localhost:8000/api/booklet/11?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJ1c2VybmFtZSI6ImFqb3NsaW4iLCJzdWIiOjUwLCJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3Q6ODAwMFwvYXBpXC9hdXRoXC9sb2dpbiIsImlhdCI6IjE0MzUwOTY4MTEiLCJleHAiOiIxNDM1MTAwNDExIiwibmJmIjoiMTQzNTA5NjgxMSIsImp0aSI6ImQ3YWJlM2NiMzY5MmQxN2JhMTBmNjVlODQ3MDMyMDA4In0.ZTYwYjJiMDA3ZDhkMjk0YWNkODdiMTg2ZmZiMDczYzgxMmJkZWVmOTBkYzhkN2E3YzA0YjhhMDE3YzVhYzNkYw
Status: HTTP/1.0 200 OK
Request Headers
Acceptapplication/json, text/plain, */*
Accept-Encodinggzip, deflate, sdch
Accept-Languageen-US,en;q=0.8,es;q=0.6
Originhttp://localhost:9000
Refererhttp://localhost:9000/
User-AgentMozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2431.0 Safari/537.36
X-DevTools-Emulate-Network-Conditions-Client-Id48A8D6C5-A31A-40CA-B8ED-9386733CA2BE
Response Headers
Access-Control-Allow-Credentialstrue
Access-Control-Allow-HeadersOrigin, Content-Type, Accept, AuthToken, Authorization, X-Authorization, X-Request-With
Access-Control-Allow-MethodsGET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin*
AuthTokeneyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJ1c2VybmFtZSI6ImFqb3NsaW4iLCJzdWIiOjUwLCJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3Q6ODAwMFwvYXBpXC9ib29rbGV0XC8xMSIsImlhdCI6IjE0MzUwOTY4MTEiLCJleHAiOiIxNDM1MTAwNDMwIiwibmJmIjoiMTQzNTA5NjgzMCIsImp0aSI6IjUwMDBjNmZiMjk3ZmU4MWJlMWE4MjZkYTU2NzVkZmMxIn0.NzE2OTA2MjBiYWVmYmY3Yzg4ZTlhMDcwNzMxYTU1NzJjYmI1NGI3NDBjNzY4MmQwMzZhYzA5MDAzMjhiYTQ5OQ
Cache-Controlno-cache
Connectionclose
content-typeapplication/json
DateTue, 23 Jun 2015 22:00:30 GMT
Hostlocalhost:8000
Set-Cookielaravel_session=eyJpdiI6Ijd3NE1lOGhFcmIydTZaWjBRUXRkXC93PT0iLCJ2YWx1ZSI6IldvMzBtUWQrdnlFbkc5bjErWEsyXC9rUXFsWkFmRElycmxxVElPdGJtekZSMTlUcHJkQVNvbnM5M1ExYlUwTU1MOSswXC8ybmMrSVgxZE9RQnUzYUpYNFE9PSIsIm1hYyI6IjVhZWRmMjUxMzNiOWRiMjJlMTU0OGZjYzM2MjdlODZmYzM5MDIxNjk4NWJiNjg0ZDU0MmNmN2RmN2E4OGIwMDEifQ%3D%3D; expires=Wed, 24-Jun-2015 00:00:30 GMT; Max-Age=7200; path=/; httponly
X-Powered-ByPHP/5.6.4

Sander Elias

unread,
Jun 24, 2015, 3:45:51 AM6/24/15
to ang...@googlegroups.com
Hi Al,

Probably those headers get attached after your interceptor. Can't you just get the token in the part of your app that does the authorization? 

Regards
Sander

Al J

unread,
Jun 24, 2015, 3:03:22 PM6/24/15
to ang...@googlegroups.com
Attached _after_ the interceptor??  Holy hole in a donut, Batman!

Well this is my 1st app using JWT tokens, and I'm using an interceptor to attach the token to each call 

All of my server calls specify an error handler that asks for a login if the error is 401 - then they reload the page

I've dozens of calls to the server, mostly $resource calls -- what do you suggest ?

Where are the rest of the headers added, certainly there must be a bottleneck somewhere after that!!

Damn!!

Al;

Sander Elias

unread,
Jun 25, 2015, 1:24:40 AM6/25/15
to ang...@googlegroups.com
Hi Al,

Ok, I did glance your question a bit too quick apparently.  You are trying to read a header from the response, not setting it before sending. Answer stays more or less the same tough ;)
Probably those headers get stripped before your interceptor. Probably your authorization tool takes out the header for security reasons. 
You can try a $http interceptor  to see if you can get the header before it reaches the $resource service.

Regards
Sander

Al J

unread,
Jun 25, 2015, 12:50:25 PM6/25/15
to ang...@googlegroups.com
I believe I am already using an $http interceptor (see below), all of my other code uses $resource -

My "auth tool" [angular-jwt] is not dealing with the response at all - it only adds the jwt to the outbound requests via an interceptor

I am still incredulous that any headers would be stripped or added at all, I imagined that the server attaches them and that's it -- everyone who looks for em can read 'em

The only thing I've ever seen was that the Allow-Headers [header] can make it impossible to see headers if they are not listed -- but even that one is gone!

This has got me flummoxed !!

al;

(echoed from 1st post)

Al J

unread,
Jun 26, 2015, 12:08:37 AM6/26/15
to ang...@googlegroups.com
I wrote the interceptor into the resource to be sure

  No change -  I still get only 

response.headers Object {content-type: "application/json", cache-control: "no-cache"}
angular.js:11699 booklet:13 was loaded

    var BookletData = $resource(
      ENV.backend+'/booklet/:id', { id: '@scriptRow.id' }, {
        get: {
          method: 'GET',
          interceptor: {
            response: function (resp) {
              console.log('response.headers', resp.headers());
              return resp;
            }
          },
          isArray: false
        }
      }
    );


 

Sander Elias

unread,
Jun 26, 2015, 1:09:10 AM6/26/15
to ang...@googlegroups.com
Hi Al,

Hmm, I just have gone over the source, and other then some mingling with the  XSRF-TOKEN the headers are put trough 1on1 as angular gets them from the browser.
The only thing I can think of is that your server returns a multipart answer. To get the  headers $http uses getAllResponseHeaders()
That would explain you seeing it in the console. but not in your interceptors.

Regards
Sander

Al J

unread,
Jun 26, 2015, 10:32:34 PM6/26/15
to ang...@googlegroups.com
I've searched on how to handle a multipart answer and can't find much - got any pointers?

I also tried to return the AuthToken in a cookie, but it doesn't arrive -- another possible symptom of a multi-part answer?

thanks
al; 

Sander Elias

unread,
Jun 27, 2015, 3:46:40 AM6/27/15
to ang...@googlegroups.com
Hi Al,

Can you try with an native xmlhttprequest, and see what you get in there. I know $http iv very convenient, but it's not the only way to communicate with a server. But check firsst with a native request, if only to see what happens...

Regards
Sander

Al J

unread,
Jun 27, 2015, 7:28:43 AM6/27/15
to ang...@googlegroups.com
$.ajax({
url: ENV.backend+'/booklet/'+bookletId,
data: { token: locker.get('jwt','no-token') },
type: "GET",
success: function(response, status, xhr){

xhr
Object {readyState: 4, responseText: "{"data":{"sequenceList":{"31":[{"branchConfig":"__…,"sequenceId":36,"sequenceName":"Demo Wrapup"}]}}", responseJSON: Object, status: 200, statusText: "OK"}

xhr.getAllResponseHeaders()
"content-type: application/json
 Cache-Control: no-cache
"

Oh %&$@#! 

  I just had a thought (wish it wouldn't hurt so much!) and realized that I'm serving locally to myself with php artisan serve (thus the URL localhost:8000)

  I better push this code up to the actual server and see if I get the proper headers back then...

sigh, i sure hope this does it...

al;


Accept-Encodinggzip, deflate, sdch
Accept-Languageen-US,en;q=0.8,es;q=0.6
Cache-Controlmax-age=0
If-Modified-SinceTue, 23 Jun 2015 22:59:32 GMT
If-None-Match"814-1435100372000"
Refererhttp://localhost:9000/
User-AgentMozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2438.3 Safari/537.36
X-DevTools-Emulate-Network-Conditions-Client-Id4461086A-A793-42FD-9B16-2CB9ED99096B
Response Headers
Access-Control-Allow-Credentialstrue
Access-Control-Allow-HeadersOrigin, Content-Type, Accept, AuthToken, Authorization, X-Authorization, X-Request-With
Access-Control-Allow-MethodsGET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin*
AuthTokeneyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJ1c2VybmFtZSI6ImFqb3NsaW4iLCJzdWIiOjUwLCJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3Q6ODAwMFwvYXBpXC9ib29rbGV0XC84IiwiaWF0IjoiMTQzNTQwMzk4NSIsImV4cCI6IjE0MzU0MDc2NTEiLCJuYmYiOiIxNDM1NDA0MDUxIiwianRpIjoiYWU0OTczMDczNGFhNGE0NjAyZWViYmVhODgxMzI4NjkifQ.ODQ1YmJlYWVlZWVjNmE1ZTNkOWRiNGQ3MmRjNjYwZTUyNTk2NDJjNjUzNDk5MDhkMzg3ODVlYmZiZWIwN2U5OQ
Cache-Controlno-cache
Connectionclose
content-typeapplication/json
DateSat, 27 Jun 2015 11:20:51 GMT
Hostlocalhost:8000
Set-CookieRefresh=eyJpdiI6InZQdW9JSjRsRHhldFlCU29MNWN2R3c9PSIsInZhbHVlIjoiMnpFdFB1dGVRVEdkMnRzZmFYVlBVeVljMmhYbUpoZzFCY1NOUDF0ZWpOc2h1YVlNQkhUMlVNNWNJNXJCRXRaaTJTMWkzZzRGMHk3U1dZZ2pKeXp6VnA2a3U3Yngxd1BzeFREdFlqK3cwWFpCMmVSbCsrYW4xc0I1clNCVW5yMmZhQUQ3a0NrWEhsRDAxQ0xGMXRhSWZwSW5waWlyUXJxYVF6NzJ2VDFrcDhVK3RxSGRLYmdLTDMzVU1qWDBqaVNxTGkzUEpjXC9la252a0loa3VjRlFPN2g4d1hOa01CMjhKemRQUkNudmFMUGhVWjhvbVJTY0FXQkR3cm9cLyswVmhZNDBBXC9haW45NHQxTFZVRzBISnhkeEU1bFU2MEhVY091MlhnY1IzQlFSWW9YUUVHQnVWb2ZZdUZQUTV4RDlDN3l3eXNpYXBhRSt2enJzVjNQMjBjNFAxWXZFS2tBWDdtVGQrZWswSEFQWlBEbWNLSGtqdkRsSlBuTjZHczBQOTN6c3NLb0lFY3cxZHZVM0o1VTRIOVdYcGJUcXY4aXMrSEFyQldxYXY4ZGZudVBBRitiMWpmaEo5UTVWV2ZVKzBJRExiN0VTTTJLYVlFQkx1clRHRG5CZlJiZm92eU1sbUt0UlFRaXpoSERVVHAzMGt4TmVEYXpNMVI3SE5mSHQrM0EiLCJtYWMiOiI0NTYxMDM3MDhhZGU3MzI2ZDdkYzVkOGYzMzlmYzZjNDU3NjA4YzRlOTBjZTY0Zjg3MWIwYmMzNTM2NTUxOWM3In0%3D; expires=Sat, 27-Jun-2015 11:21:51 GMT; Max-Age=60; path=/; httponly
Set-Cookielaravel_session=eyJpdiI6Ik51YU01RkNwMWZCc2xMdkVuV2d6bkE9PSIsInZhbHVlIjoib0xoMlJid2hKSkFTdGtXK3dHZUJNM3lpWFNFYlBINlRzSFJYTFRXbVZ2VGswdXphYVFTemRDYngxWk5ZZmJBbUY1dTRaOHRcL01WeDVkaCtDMjN6bGhRPT0iLCJtYWMiOiJiZDYzMmM5NDlhYTE0MWEzNjcyNDJlZDdiZTI0NWY0ZWM5ZGE2NWQ1MWE0NTBmZDE1MTYxM2YwM2NjMTc1NDlhIn0%3D; expires=Sat, 27-Jun-2015 13:20:51 GMT; Max-Age=7200; path=/; httponly
X-Powered-ByPHP/5.6.4

Al J

unread,
Jun 27, 2015, 9:29:59 AM6/27/15
to ang...@googlegroups.com
%$#@^$!

It works on the real server...

Sorry to take you down the rabbit hole with me Sander

Seems like I'm always dragging you somewhere...

So, for the record: using a backend of Laravel4 being served locally via: php artisan serve -- returning headers/cookies is not guaranteed..

Thanks,
Al;
Reply all
Reply to author
Forward
0 new messages