NodeJS从HTTP响应中读取文件附件 [英] NodeJS Read file attachment from HTTP response

查看:462
本文介绍了NodeJS从HTTP响应中读取文件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何读取或访问HTTP响应的附件吗?

Does anyone know how to read or get access to the attachement of an HTTP response?

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

I mean, the server response is:

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文件的内容在哪里?

But where is this mybill-234324.pdf file content???

我的代码:

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();

推荐答案

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

I ran into the same issue where I tried various npm packages to get the content of the file which has

content-disposition:附件

content-disposition: Attachment

  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

这篇关于NodeJS从HTTP响应中读取文件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆