Response Cookie not getting set in Kohana - kohana

I'm trying to pull out the value of a cookie from the response of an external request in Kohana 3.2
$response = Request::factory('http://myurl')->execute();
echo $response->cookie('cookie');
Now in my example above, the server response from 'http://myurl' is setting the cookie cookie. In fact, if I do print_r($response->headers()); I can see the cookie being set in the set-cookie header.
But yet when I just try to access the cookie from $response->cookie('cookie'); I don't get anything.
Is there something I'm doing wrong?

I had exactly the same problem - I solved it using $response->headers('Cookie')

Related

Response header Set-Cookie doesn't store cookies in browser

I'm setting 2 cookies in response from backend to the browser.
One that is secure HTTPOnly (it's refreshToken) and the other one without those parameters so it's accessible to JavaScript (carrying information about refreshToken is set and when it expires).
Cookies in response:
Neither of those two is set in browser. I studied all about cookies and I'll be honest, I'm lost here and need your help.
At first it was working very well on my localhost environment (backend on localhost:8080, frontend on localhost:3000).
Then I made deploy and there it was NOT working.
I tested again on localhost and I checked "Disable cache" to prevent unwanted behaviour and it is not working even there.
I'll mention that I'm using CORS, not sure if that can may interfere:
I tested this both in Chrome and Firefox.
I FINALY figured it out. Cookies are valid. The answer is to set withCredentials = true both in frontend and backend.
First I thought the parameter withCredentials on frontend is used only when we want to send our credentials hidden in secure HttpOnly cookies to which we don't have access to but browser does.
Apparently it is also used when we want to set cookies from response. Link to docs.
responses from a different domain cannot set cookie values for their own domain unless withCredentials is set to true before making the request
Secondly, we also have to add withCredentials on backend to CorsFilter (in Spring boot) otherwise we get CORS error.
CorsConfiguration()
.apply {
allowedOrigins = listOf(uiUrl)
allowedHeaders = listOf("*")
allowedMethods = listOf("*")
allowCredentials = true
}

Getting Clients Cookies in Node HTTP Server

I am trying to set/get cookies for users that browse on my web server, I found the following StackOverflow question: Get and Set a Single Cookie with Node.js HTTP Server and I was able to get the cookie set on the browser just fine. When I go to the cookie viewer I see the cookie I set just as I want it. The problem comes when I try to view the cookies, the request.headers.cookie is always undefined. How would I go about getting the cookies on a users browser, preferably without NPM modules and purely node and its own modules?
How I'm setting the cookie (this works fine, I am able to see this cookie in the browser when I go to view my cookies):
response.writeHead(statusCode, {
'Set-Cookie': cookie
})
// statusCode = 200
// cookie = 'token=SOME_TOKEN'
// In the browser I see the exact cookie I set
How I'm trying to get the cookie (not working always undefined):
let cookies = request.headers.cookie
// This returns undefined always
// even when I can view the cookie in the
// browser the request is coming from
// Also quick note: I'm currently not
// parsing the cookies out to view them as
// a JSON object because I can not get any
// of the cookies
EDIT:
It seems I have finally found the error, it sets the cookie path to be whatever path I set the cookie on, so I was setting the cookie on the "/auth" route. How can I make it so that this cookie is accessible from any route the user goes to?
Ok I finally found the solution, my error was that it was auto-setting the path of the cookie to be "/auth" so I could only access the cookie if the url requested contained "/auth", where I set the cookie I changed it to the following:
response.writeHead(statusCode, {
'Set-Cookie': cookie + '; Path=/'
})
And now I can access my cookie

Cookies missing when server issues redirect using ClientSession

A good example would be a site login: POST user credentials and a session cookie would be set (Set-Cookie) with a 302, but after following the redirect neither ClientResponse.cookies, ClientResponse.history[-1].cookies, nor ClientSession.cookie_jar contain the session cookie. Is this an error in aiohttp? Note, I do see the cookie in ClientResponse.history[-1].headers.
Seems as this is more an issue with SimpleCookie
https://bugs.python.org/issue23930
https://bugs.python.org/issue31456

Express 4, how to see all cookies being sent in res

Hi all i'm using Express 4 with cookie-parser. For some reason the cookie is being set in the res object, but is not really stored in the browser. I wanted to see ALL cookies that are about to be passed at the end (all cookies for the current response object). Any idea on how to get them?
Use this:
console.log('Cookies: ', res._headers["set-cookie"]);
Explanation: As the cookies are set in the response header as "set-cookie" attribute.

cookie in redirected response

As shown in the image below, I am trying to get the set-cookie in the first redirect#1 response.
I succeed to get the response with request module by setting followRedirect to be false in the request options, but I still could not get the set-cookie from the header.
A similar discussion here: https://github.com/request/request/issues/1502
Anyone has managed to use any other module to get the cookie from redirected response? Thanks a lot in advance!
response.headers['set-cookie'] should return the values you want.

Resources