Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 2 http withCredentials

I'm am trying to use withCredentials to send a cookie along to my service but can't find out how to implement it. The docs say "If the server requires user credentials, we'll enable them in the request headers" With no examples. I have tried several different ways but it still will not send my cookie. Here is my code so far.

private systemConnect(token) {     let headers = new Headers();     headers.append('Content-Type', 'application/json');     headers.append('X-CSRF-Token', token.token);     let options = new RequestOptions({ headers: headers });     this.http.post(this.connectUrl, { withCredentials: true }, options).map(res => res.json())     .subscribe(uid => {         console.log(uid);     }); } 
like image 962
Lindstrom Avatar asked Jul 27 '16 14:07

Lindstrom


People also ask

How would you write code to modify the response from an HTTP GET?

catch( (error: Response) => { return Observable. throw(error); } );

What does withCredentials do in angular?

withCredentials property is a boolean value that indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies, authorization headers or TLS client certificates.

What is HttpRequest in angular?

HttpRequest represents an outgoing request, including URL, method, headers, body, and other request configuration options. Instances should be assumed to be immutable. To modify a HttpRequest , the clone method should be used.


1 Answers

Try to change your code like this

let options = new RequestOptions({ headers: headers, withCredentials: true }); 

and

this.http.post(this.connectUrl, <stringified_data> , options)... 

as you see, the second param should be data to send (using JSON.stringify or just '') and all options in one third parameter.

like image 88
Oleg Barinov Avatar answered Sep 18 '22 09:09

Oleg Barinov