$http response cookies?

16,573 views
Skip to first unread message

Stas Sarosek

unread,
Apr 27, 2012, 7:09:22 AM4/27/12
to AngularJS
Hello.
Please tell me how to read a cookies in the response method
$http(POST, GET)?

Vojta Jína

unread,
Apr 29, 2012, 8:33:13 AM4/29/12
to ang...@googlegroups.com
You can read cookies using $cookies service
http://docs-next.angularjs.org/api/angular.module.ngCookies.$cookies

The cookies from server are set in headers, you can read them:
$http.get('/some').success(function(data, status, header) {
console.log(header('cookie');
});

Check out http://docs-next.angularjs.org/api/angular.module.ng.$http

V.
> --
> You received this message because you are subscribed to the Google Groups "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to angular+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/angular?hl=en.
>

sarose...@gmail.com

unread,
Apr 29, 2012, 5:15:16 PM4/29/12
to ang...@googlegroups.com
I checked:
$http.post(url, key, config).success(
                function (data, status, header) {
                    console.log(header('Set-Cookie'));
});
But I get a response null.


воскресенье, 29 апреля 2012 г., 16:33:13 UTC+4 пользователь Vojta Jína написал:
You can read cookies using $cookies service
http://docs-next.angularjs.org/api/angular.module.ngCookies.$cookies

The cookies from server are set in headers, you can read them:
$http.get('/some').success(function(data, status, header) {
  console.log(header('cookie');
});

Check out http://docs-next.angularjs.org/api/angular.module.ng.$http

V.

On Fri, Apr 27, 2012 at 1:09 PM, Stas Sarosek <sarose...@gmail.com> wrote:
> Hello.
> Please tell me how to read a cookies in the response method
> $http(POST, GET)?
>
> --
> You received this message because you are subscribed to the Google Groups "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to angular+unsubscribe@googlegroups.com.

Vojta Jína

unread,
May 12, 2012, 7:47:34 PM5/12/12
to ang...@googlegroups.com
Be sure to check the response headers in Web Inspector, may be your
server does not send the cookies...

V.
>> > angular+u...@googlegroups.com.
>> > For more options, visit this group at
>> > http://groups.google.com/group/angular?hl=en.
>> >
>
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/angular/-/X8KYFGlW0QkJ.
>
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
Message has been deleted
Message has been deleted

sarose...@gmail.com

unread,
Jun 5, 2012, 3:28:28 PM6/5/12
to ang...@googlegroups.com
I immediately checked with the Web Inspector (for example: FireBug in Firefox), and the response from the server the parameter cookies with the value was present in headers. May be you have any ideas what's the problem?

Misko Hevery

unread,
Jun 6, 2012, 8:38:47 PM6/6/12
to ang...@googlegroups.com
Can JS access the cookies? Server has to set a flag to allow js to access cookies, otherwise no cookie for you. ;-)

On Tue, Jun 5, 2012 at 12:28 PM, <sarose...@gmail.com> wrote:
I immediately checked with the Web Inspector (for example: FireBug in Firefox), and the response from the server the parameter cookies with the value was present in headers. May be you have any ideas what's the problem?

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/ZbDr8K2ypY0J.

Chris Bolton

unread,
Jan 16, 2013, 8:45:48 PM1/16/13
to ang...@googlegroups.com, mi...@hevery.com
what flag is that?

Misko Hevery

unread,
Jan 17, 2013, 1:45:48 AM1/17/13
to Chris Bolton, ang...@googlegroups.com

Chris Bolton

unread,
Jan 17, 2013, 2:23:53 AM1/17/13
to Misko Hevery, ang...@googlegroups.com
na I still have access to document.cookie but it doesn't show up in headers('Set-Cookie').

Dmitry Maevsky

unread,
May 13, 2013, 6:12:59 PM5/13/13
to ang...@googlegroups.com, sarose...@gmail.com
Hello, I get exactly the same issue: the Set-Cookie header does not seem to show in $http.post(...).success, and the $cookies service does not return the updated cookie either, though I can see it all right in web inspector, and it is NOT httpOnly or secure:

var MainCtrl = ['$scope', '$http', '$cookies', function($scope, $http, $cookies) {
  $scope.logout = function() {
    $http.post('/action/logout').success(function (data, status, header) {
      console.log(header('Set-Cookie'));
      console.log($cookies);
    });
  };
}];

The first console.log outputs null, and the second outputs the old cookie. I ended up solving the issue by doing $rootScope.$cookies = $cookies during initialization (in module.run) and then adding a $scope.$watch("$cookies['mycookie']", function(newValue) { doWhatINeedToDo(newValue); });   in the controller.
So it is just to let you know that $cookies is NOT updated in $http interceptors, which is quite weird.

Dmitry

Luis De Avila

unread,
Jun 13, 2013, 8:36:43 PM6/13/13
to ang...@googlegroups.com, sarose...@gmail.com
I'm also unable to access response cookies. I can see the cookie in the Chrome dev console.

I'm using Express as my backend and this is the snippet of code I'm using to set the cookie on the response:

                    res.cookie('test', 'test', { expires: new Date(Date.now() + 900000), httpOnly: false });
                    return res.send(data);

And here is my service:

authModule.factory('authenticationService', ['$cookies', '$http', function ($cookies, $http) {
    var AuthService = {
        login: function (email, password) {
            var config = {
                method: 'POST',
                url: 'http://localhost:4000/users/',
                data: {
                    email: email,
                    password: password
                }
            };
            //TODO: Implement some better non 200 status code handling
            var promise = $http(config).then(function (response) {

                console.log($cookies.test);
                console.log(response.headers('Set-Cookie'));
                console.log(response.headers());

                var status = response.status;
                switch (status) {
                    case 200:
                        return response.data;
                        break;
                };
            });
            return promise;
        }

The console shows:

Object {content-type: "application/json; charset=utf-8"}

Anyone know what they heck is going on? I'm also surprised I can't see more header values.

Thanks for the help!


On Friday, April 27, 2012 4:09:22 AM UTC-7, Stas Sarosek wrote:

Luis De Avila

unread,
Jun 13, 2013, 10:33:42 PM6/13/13
to ang...@googlegroups.com, sarose...@gmail.com
Solved the issue. This post gave me a clue: https://groups.google.com/forum/#!topic/angular/CqVKKVxE8zI

The issue was that I was running two servers on two different ports on the same domain... and that is a cookies no-no. Oopsie.

Andrew Del Prete

unread,
Oct 3, 2013, 5:16:30 AM10/3/13
to ang...@googlegroups.com, sarose...@gmail.com
I know it's been a long time since you posted this here. But I am fairly new to Angular and I'm having issues with Cookies. I am using ngCookies and the $http service to post some information and retrieve a cookie from the server. I am retrieving the cookie just fine. My $http service is setting a $location.path() on Success and I'm checking for this cookie in the new controller it goes to. My problem is my controller doesn't recognize the cookie initially, however if I go to one more additional route, the cookie is then seen... If I am looking at my Resources tab under inspector, the cookie doesn't not show up until I click on another tab and then back on Resources. It's like my browser needs an additional event of some sort for the cookie to be viewable.

Any ideas out there? Thank you

alec...@gmail.com

unread,
Oct 17, 2013, 3:13:23 PM10/17/13
to ang...@googlegroups.com, sarose...@gmail.com
Unfortunately, this is still a problem in Angular 1.2.0-RC2.

In general, I can manage cookies using $cookies just fine. However, inside the success handler of a post(), $cookies returns old cookie value, and not the new value from the post response.

Alec

Seth Martin

unread,
Jan 15, 2014, 12:32:28 PM1/15/14
to ang...@googlegroups.com, sarose...@gmail.com
 inside the success handler of a post(), $cookies returns old cookie value

This is the problem that I am having as well. There is something that I am missing. Does anyone have any ideas? I am using version 1.2.6

$http.post('/wms/x-services/signin', tokens)
.success(function(result, status, headers, config) { // sign in successful
// do stuff with $cookies -- but the $cookies are not updated with the response
})
.error(function(data, status, headers, config) {
});

Seth Martin

unread,
Jan 15, 2014, 2:10:58 PM1/15/14
to ang...@googlegroups.com, sarose...@gmail.com
The solution I chose to use based on the information found here and here. Tells me that the $cookies will attempt to refresh every 100ms. So with this information in mind you can add a $timeout to any request that needs to use the response cookies.

$http.post('/wms/x-services/signin', tokens)
.success(function(result, status, headers, config) { // sign in successful
$timeout(function() { /* do stuff with $cookies here. */ }, 100);
})
.error(function(data, status, headers, config) {
});

Elizabeth Bastian

unread,
Aug 19, 2014, 1:38:57 PM8/19/14
to ang...@googlegroups.com
I know this is old but I just ran into this and the $timeout workaround works perfectly for me.  I'm using the latest version of angular. Is this something that is going to be changed eventually?  Or has anybody found another way around this?

Santosh Tadikonda

unread,
Mar 4, 2015, 8:37:47 PM3/4/15
to ang...@googlegroups.com
I used the timeout solution in Angular 1.2.8 and it worked. I immediately upgraded my Angular version to 1.3.14 and it is broken :( Does any one know a fix ?
Reply all
Reply to author
Forward
0 new messages