NodeJS从HTTP响应中读取文件附件

时间:2015-05-01 10:45:41

标签: node.js http

有谁知道如何阅读或访问HTTP响应的附件?

我的意思是,服务器响应是:

HTTP/1.1 200 OK
Server: something
Date: Fri, 01 May 2015 10:22:29 GMT
Content-Type: application/pdf
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Language, Cookie
X-Frame-Options: SAMEORIGIN
Content-Language: en
Content-Disposition: attachment; filename=mybill-234324.pdf
Set-Cookie: s=xxxxx; expires=Fri, 15-May-2015 10:22:29 GMT; httponly; Max-Age=1209600; Path=/; secure
Strict-Transport-Security: max-age=15552000

但这个mybill-234324.pdf文件内容在哪里???

我的代码:

var req = https.request({
    method: 'GET',
    host: 'thehost.ext',
    path: '/account/bills/' + number + '/',
    headers: {
      'Cookie': 's=supercryptedhash;',
      'Accept-Language': 'en-US,en'
    }
  }, function(res) {
    // ????
  });

  req.on('error', function(err) {
    console.error('Request Error', err);
  });

  req.end();

2 个答案:

答案 0 :(得分:0)

https文档(https://nodejs.org/api/https.html)提供了一个示例:

options = { ... };

var req = https.request(options, function(res) {
    console.log("statusCode: ", res.statusCode);
    console.log("headers: ", res.headers);

    res.on('data', function(data) {
        // var `data` is a chunk of data from the response body
        process.stdout.write(data);
    });
});


req.on('error', function(e) {
  console.error(e);
});

req.end();

答案 1 :(得分:0)

我遇到了同样的问题,我尝试了各种npm软件包来获取具有以下内容的文件的内容

内容处置:附件

  const url = "https://XXXX"; // your URL Here
  const curl = require('curl');
  let options = {};  
  curl.get(url, options, function(err, response, body) {          
  console.log("BODY", body);      
  });

最后我使用CURL

得到了答复