2017-09-01 30 views
0

我嘗試使用https.request,但是當我想要訪問cookie響應時,這不會出現在資源頭部。 HTTP/HTTPS。無法使用HTTP/HTTPS的Nodejs獲取cookie

示例代碼:

let https = require('https'); 

https.request({ 
    rejectUnauthorized: false, 
    hostname: 'example.com', 
    port: '443', 
    path: '/example.html', 
    method: 'POST', 
    headers: { 
     "Connection": "keep-alive", 
     "Accept-Encoding": "gzip, deflate, br", 
     "Upgrade-Insecure-Requests": "1", 
     "Content-Type": "application/x-www-form-urlencoded", 
     "Cache-Control": "no-cache", 
     "Pragma": "no-cache", 
     "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", 
     "Accept-Language": "es-ES,es;q=0.8", 
    } 
}, (res: any) => { 
    console.log('headers', res.headers); // this does not show 'set-cookie' property. 
}) 

得餅乾支持嗎? 謝謝

P.S:我使用typescript來編寫,ts-node來運行這個腳本。

+0

請向我們展示您正在使用的代碼,以便我們可以清楚地看到您在做什麼。 Cookies完全支持正確的代碼。 – jfriend00

+0

對不起,感謝您的讚賞! –

+0

'res.statusCode'的價值是什麼? – robertklep

回答

1

你可能想從NPM餅乾解析器包,並添加

const cookieParser = require('cookie-parser'); 

您app.js文件的頂部,並

app.use(cookieParser()); 

到您的中間件。 Cookies隨後將以req.cookies的形式提供。

+0

我認爲OP正在用'https.request()'發出一個請求,並希望從該響應中獲取與您在答案中說明的內容不同的響應。 – jfriend00