Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting request cookies angular2 http post request

I have a login service that performs an http request (to a php backend server) and returns a cookie. After login this cookie is needs to be used in all further requests done by the client.

loginservice:

   login(userCredentials:UserCredentials):Observable<any> {    
        this.request.ServiceType = "Connect"
        this.request.SessionId = "sessionlessrequest"
        this.request.Compression = "no"
        this.request.Parameters = userCredentials;
        let jsonRequest = JSON.stringify(this.request)
        return this.http.post("http://localhost/request.php", "data="+ jsonRequest,
            { headers: new Headers({
                'Content-Type': 'application/x-www-form-urlencoded'
            })
            }).map( (responseData) => {
            this.apiResponse = responseData.json() as ApiResponse
            if (!this.apiResponse.Error) {
                this.sessionData.next(this.apiResponse.Data as UserSessionData)
                this.sessionData.asObservable().share
            } else {
                console.log(this.apiResponse.ErrorMessage)
            }
            return null
        })

When doing this call, the cookie response is like this:
getcookie

I see that I get the session_id from session_start (on my php server)

I when doing the request :

searchExams(searchObject:SearchObject):Observable<any>{
        let headers = new Headers();
        headers.append('Content-Type', 'application/x-www-form-urlencoded',)
        let options = new RequestOptions({ headers: headers, withCredentials: true});
        this.request.ServiceType = ServiceType.SearchExams;
        this.request.SessionId = this.userSessionData.SessionId;
        this.request.Compression = "no"
        this.request.Parameters = searchObject;
        let jsonRequest = JSON.stringify(this.request)
        console.log(jsonRequest)
        return this.http.post("http://localhost/request.php", "data="+ jsonRequest, options
            ).map( (responseData) => {
            this.apiResponse = responseData.json() as ApiResponse
            if (!this.apiResponse.Error) {
....

I see that the request cookie is totally different from the one I got from the server. Moreover it is always the same PHPSESSID postrequest

Do I need to set the request cookie? How? What am I missing?

Thanks....

like image 479
Maniac_1979 Avatar asked Oct 27 '16 14:10

Maniac_1979


1 Answers

Try this for the login:

(...)
return this.http.post("http://localhost/request.php", "data="+ jsonRequest,
    { withCredentials: true,
        headers: new Headers({
        'Content-Type': 'application/x-www-form-urlencoded'
    })
    }).map(...)

And the other requests:

(...)
return this.http.post("http://localhost/request.php", "data="+ jsonRequest,
                      {withCredentials: true}).map(...)

Basically, just use withCredentials option set to true.

like image 134
AArias Avatar answered Nov 19 '22 02:11

AArias