3

Api works perfectly when called from advanced client rest but when I called through http it doesn't work as expected. This is because it is not sending the credentials. Guys help me how can I use here withCredentials:true.

Here is my angular code :

var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post('http://localhost/jiffy_fun/laravel/public/token',{headers: headers}).map(res => res.json()).subscribe(res => {this.token = res});

Thanks

Sangwin Gawande
  • 7,658
  • 8
  • 48
  • 66
vikram mistry
  • 106
  • 1
  • 7

3 Answers3

2

I think that you mix two different things. For what I saw in the code you provided, you want to send credentials from a form using an AJAX request using Angular2 HTTP class. In this case, you need to provide this content within the second parameter of the post method, as described below:

var creds = "username=" + username + "&password=" + password;

var headers = new Headers();
headers.append('Content-Type',
         'application/x-www-form-urlencoded');
this.http.post(
    'http://localhost/jiffy_fun/laravel/public/token',
    creds,
    {headers: headers})
.map(res => res.json())
.subscribe(res => {this.token = res});

The withCredentials attribute is something different related to CORS (cross domain requests). In fact, by default, CORS doesn't send cookies for such requests. You can choose to change this behavior by setting the withCredentials attribute to true on the xhr object. This link could give you some additional hints: http://www.html5rocks.com/en/tutorials/cors/?redirect_from_locale=fr. See this withCredentials section.

Hope it helps you, Thierry

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • I am getting the required result in advanced client rest but not through angular. Actually in each request which is made by angular it creates a new session file but with advance client it simple update the current session. Please help... – vikram mistry Jan 11 '16 at 08:45
  • Which REST client do you use? Could you give us in your question what you put in this client (headers, payload, HTTP method)? Thanks! – Thierry Templier Jan 11 '16 at 08:49
  • Its advanced client rest I have mentioned in my question – vikram mistry Jan 11 '16 at 08:51
  • 1
    Okay got it! Could you update your question to the content of both request and response from REST advanced clients (headers, payload, HTTP method)? Thanks! – Thierry Templier Jan 11 '16 at 09:07
1

Couple things spring to mind - you were doing a POST but sending the headers as body.

And with withCredentials can be set along with headers like below. Sorry haven't time to plunkr just now :S

let headers = {
  'Content-Type' : 'application/x-www-form-urlencoded'
}
let options = new RequestOptions({ headers: headers, withCredentials: true });

this.http.post('http://localhost/jiffy_fun/laravel/public/token',JSON.stringify({}),options).map(res => res.json()).subscribe(res => {this.token = res});

I'm not sure how http API will cope with the empty url encoded body so opted for the safest option of stringifying empty object - but sure this could be tidied up!

Hope helps!

librsean
  • 83
  • 9
0

You are missing body parameter in your http post call.

// Http post function declaration
post(url: string, body: string, options?: RequestOptionsArgs) : Observable<Response>

https://angular.io/docs/ts/latest/api/http/Http-class.html

TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125