Вот мои команды для использования файлов cookie для curl:
# testing to see before login
curl http://localhost:3000/me
# undefined
# login
curl --cookie cookies.txt --cookie-jar cookies.txt \
-X POST -H 'Content-type: application/json' \
--data '{"username":"blahblahblah"}' \
http://localhost:3000/login
# OR--
# curl --cookie cookies.txt --cookie-jar cookies.txt \
# -X POST -H 'Content-type: application/x-www-urlencoded' \
# --data 'username=blahblahblah' \
# http://localhost:3000/login
# verify that we are logged in
curl --cookie cookies.txt http://localhost:3000/me
# blahblahblah
# download the file, (usually curl http://thing/file > file)
curl --cookie cookies.txt --cookie-jar cookies.txt \
http://localhost:3000/download
# helloworldfile
и пример приложения, которое я разработал для его тестирования:
var express = require('express');
var session = require('express-session');
var app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: {}
}));
app.get('/login', (r, s) => s.send(`<html>
<body>
<form method="post">
<input type="text" name="username" />
<input type="submit" />
</form>
</body>
</html>`));
app.post('/login', (req, res) => {
var { username } = req.body
if (username && username.length && username.length > 5) {
req.session.login = username;
res.end();
} else {
res.end('bad login\n');
}
});
app.get('/me', (r, s) => s.send(r.session.login + '\n'));
app.get('/download', (req, res) => {
if (!req.session.login)
return res.send('bad login\n');
res.set({
'Content-Description': 'File Transfer',
'Content-Type': 'text/plain',
'Content-Disposition': 'attachment; filename="data.txt"',
'Content-Transfer-Encoding': 'binary',
'Expires': '0',
'Cache-Control': 'must-revalidate, post-check=0, pre-check=0',
'Pragma': 'public',
'Content-Length': '14'
});
res.end('helloworldfile');
});
app.listen(3000);