Express 4, how to see all cookies being sent in res - node.js

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.

Related

How to get response header for same key values

I try to extract data out of a website using requests.
I am particularly interested in response headers for key "Set-cookie".
However, when I check in Chrome Developer Tool there are multiple values for the same key in response header.
I know in requests I just need to issue response.headers to get all headers.
How to get all values if I have multiple same keys like "Set-Cookie" ?
To retrieve all cookies from a requests response, use cookies attribute.
import requests
res = requests.get("http://example.com")
for cookie in res.cookies:
print(cookie.name, cookie.value)

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

read cookie age from http request in server side

req.cookies :
When using cookie-parser middleware, this property is an object that
contains cookies sent by the request. If the request contains no
cookies, it defaults to {}.
// Cookie: name=tj
req.cookies.name
// => "tj"
This is what i found in the official documentation , However, it is expected that req.cookies.name returns an object contains all info about cookie , Not ONLY STRING which is the value of cookie .
Expected
req.cookies.name ==> {value:"e3Lfdsd3pd1...er",expiration:...,..:...}
Actual
req.cookies.name ==> "e3Lfdsd3pd1...er"
How to retrieve other info of cookies than its value using request object ?
Is there something ready in express or cookie-parse, Or have i to use Nodejs built-in API?
You can not access this data because it's simply not there.
The browser sends only the key-value pairs.
expires and max-age are local informations for the browser only and will not be committed to a web server in general.
You can set those attributes on cookie creation on the server, also you can overwrite them later (e.g. for invalidating), but I'm afraid you can't read the values of those attributes.

Set Cookie fails in ExpressJs

I have a problem in Express 4.x.I can't set any cookies. I want to set a cookie via Ajax request, i do the request, my server-side res.cookie() is executed, in my response headers i can find my 'set-cookie' header with the specific name and value, but nothing happens, the cookie is not registered. I have no HttpOnly or secure flag.
Cookie set example :
res.cookie('my_cookie','value',{maxAge:500,secure:false,httpOnly:false});
What i've noticed is that if i set maxAge 500 for example, my cookie expiration date from response headers is about 5 hours ago, so i tried to add a bigger value, but nothing happened.
It is like my set-cookie header is ignored. I don't use AngularJS, just jQuery.
If i put the set-cookie content in document.cookie from js console, the cookie is registered..
Thanks in advance and sorry for my bad english .
Version 4.0 removed a lot of the convenience middleware from the library for a more modular architecture; in this case you need to reference the cookie-parser middleware. Without it req.cookies is just an empty {}.
$npm install cookie-parser
var express = require('express')
var cookieParser = require('cookie-parser')
var app = express()
app.use(cookieParser())
You also need it to sign cookies:

Response Cookie not getting set in 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')

Resources