Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$http doesn't send cookie in Requests

We are working on a RESTful Webservice with AngularJS and Java Servlets. When the user logs in, our backend sends a "Set-Cookie" header to the frontend. In Angular we access the header via $cookies (ngCookie - module) and set it. enter image description here

Now that the user is logged in he can for example delete some stuff. Therefore the frontend sends a GET request to the backend. Because we work on different domains we need to set some CORS Headers and Angular does an OPTIONS request before the actual GET request:

OPTIONS request: OPTIONS request

GET request GET request

We do this in Angular via $http module, but it just won't send the cookie, containing JSESSIONID.

How can I enable Angular to send cookies?

like image 249
Tim Daubenschütz Avatar asked Jun 12 '13 11:06

Tim Daubenschütz


People also ask

Why is cookie not sent with request?

If the server doesn't allow credentials being sent along, the browser will just not attach cookies and authorization headers. So this could be another reason why the cookies are missing in the POST cross-site request.

Are cookies sent with POST request?

After receiving an HTTP request, a server can send one or more Set-Cookie headers with the response. The browser usually stores the cookie and sends it with requests made to the same server inside a Cookie HTTP header.

How do I add a cookie to a GET request?

To add cookies to a request for authentication, use the header object that is passed to the get/sendRequest functions. Only the cookie name and value should be set this way. The other pieces of the cookie (domain, path, and so on) are set automatically based on the URL the request is made against.

How are HTTP cookies sent to the server?

After receiving an HTTP request, a server can send one or more Set-Cookie headers with the response. The cookie is usually stored by the browser, and then the cookie is sent with requests made to the same server inside a Cookie HTTP header. An expiration date or duration can be specified, after which the cookie is no longer sent.

Can an HTTP response include multiple Set-Cookie headers?

An HTTP response can include multiple Set-Cookie headers. The client returns multiple cookies using a single Cookie header. The scope and duration of a cookie are controlled by following attributes in the Set-Cookie header: Domain: Tells the client which domain should receive the cookie.

How do I set a cookie in a request?

To set a cookie, the server includes a Set-Cookie header in the response. The format of a cookie is a name-value pair, with optional attributes. For example: Here is an example with attributes: To return a cookie to the server, the client includes a Cookie header in later requests.

What happens when a server receives an HTTP request?

After receiving an HTTP request, a server can send one or more Set-Cookie headers with the response. The browser usually stores the cookie and sends it with requests made to the same server inside a Cookie HTTP header.


1 Answers

In your config, DI $httpProvider and then set withCredentials to true:

.config(function ($httpProvider) {     $httpProvider.defaults.withCredentials = true;     //rest of route code }) 

Info on angularjs withCredentials: http://docs.angularjs.org/api/ng.$http

Which links to the mozilla article: https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control#section_5

like image 168
Mathew Berg Avatar answered Oct 08 '22 07:10

Mathew Berg