【问题标题】:How to download file using curl with username/password and cookies?如何使用带有用户名/密码和 cookie 的 curl 下载文件?
【发布时间】:2020-05-27 19:57:12
【问题描述】:

我正在尝试从安全 HTTP 下载数据。我需要传递 cookie 才能通过登录并下载数据。我试试:

curl --u "用户名" --d "https://url.datasite.login" --cookie-jar auth.cookies

curl --cookie auth.cookies -O "https://url.datalocation.file"

但是在运行第一个命令后,我得到了:

curl:没有指定 URL!

我尝试过重新排列顺序、更改引号、使用 作为用户名、将用户名和 url 设置为环境变量等。似乎都没有成功。还将 shell 从 tcsh 更改为 bsh,并尝试使用“wget”代替它检索 400 错误。

【问题讨论】:

    标签: url curl https tcsh


    【解决方案1】:

    这是我使用 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);
    

    【讨论】:

    • 我正在下载数据文件,是否必须更改“内容类型”?这也是针对http的,受保护的https有什么不同吗?您的代码产生:错误:错误操作
    猜你喜欢
    • 2011-02-05
    • 2014-10-16
    • 2019-01-02
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    相关资源
    最近更新 更多