How to send Cookie with DS.RESTAdapter?


#1

I’m building an Ember demo to convince our company to stop building .jsp pages and start building the front end with Ember! :grinning:

To convince them, I need to use our existing APIs for data in my demo. I’m not able to get a session cookie through an API. In the current site the session is created with direct a java call . To get around this, I’m trying to send a cookie with the GET request. However, I get a error: "Refused to set unsafe header “Cookie”. How can I send a cookie with a request?

 // adapters/account.js
export default DS.RESTAdapter.extend({
     host: 'http://localhost:8080/api/v2',
     headers: computed('session.authToken', function() {
  return {
     'Cookie': 'JSESSIONID=A05E839C3BDBF9D7F2B3564E2F598B7B'
    };
   })
  });

#2

Ideally you would switch from cookie based authentication to a form of JSON Web Tokens.

If you want to keep using the cookie though, you should just set a cookie in JS before making the call, and it should get included with the request automatically. You could do this directly in JS or use something like the Ember-Cookies addon.


#3

@Mofungo to send cookies (assuming a cross domain request), you will need to set up sendCredentials for the XHR request:

ajax(url, method, hash = {}) {
    hash.xhrFields = { withCredentials: true };

    return this._super(url, method, hash);
}