1

I am working with angularjs - 1.6.

I need to send one http GET request to server along with a cookie explicitly.

I mean, I know once we get authorization done successfully, cookies are being sent with every http request automatically.

But in my case, I need to send it explicitly. How can I achieve so. Below is the https code:

$cookies.put('JSESSIONID', 'lu5xxkdqgjk5qpv07ufhvln3');
 $http({
         url:"http://10.11.0.11:4440/api/21/projects",
          method:"GET",
          headers: { 'Accept': 'application/json',
                     'X-Rundeck-Auth-Token':'lu5xxkdqgjk5qpv07ufhvln3'},
         "Access-Control-Allow-Credentials" : true
      }).then(function(response) {
          $scope.response = response.data;
          alert("Report fot succeed"+response.data);
      }, function(response) {
          alert("error response:"+response.data);
      }); 
Puneet
  • 615
  • 2
  • 7
  • 17
Anubha Gupta
  • 189
  • 2
  • 17

1 Answers1

1

You can do it in several ways.

You can send value of your cookie as one of header if you insist that it should be GET:

headers: {'Cookie': 'Value', ...}

Of course you can previously get value of cookie then assign it to some variable and then set 'Cookie' header to this value.

headers: {'Cookie': myVariable, ...}

Remember to set

 $httpProvider.defaults.withCredentials = true;

In .config file.

If data which will be sent is more complex you should consider changing GET to POST as POST is mentioned to send data, for example:

   this.login = function (loginInfo) {

        return $http({
            url: 'your url',
            headers: {
                'Content-Type' : 'your content type'
                // other headers..
            },
            method: 'POST',
            data: {
                user: {
                    name: loginInfo.nick,
                    password: loginInfo.password
                }
            }
        })
        .then(function(response) {
             console.log('success');
        });
   }
BT101
  • 3,666
  • 10
  • 41
  • 90
  • If I set cookie in header then I get this error: angular.min.js:108 Refused to set unsafe header "Cookie" – Anubha Gupta Mar 19 '18 at 08:14
  • Did you set `$httpProvider.defaults.withCredentials = true;` in config? – BT101 Mar 19 '18 at 10:05
  • Why don't you just send post it seems weird to me to send data from cookie in header anyway, what exactly you've got inside this cookie? Maybe you should use interceptor if for example you store token in this cookie. – BT101 Mar 19 '18 at 10:17
  • You should also change angular.min.js to angular.js for development to see more descriptive error info – BT101 Mar 19 '18 at 13:43