3

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

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

My code:

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();
G. Ghez
  • 3,429
  • 2
  • 21
  • 18

2 Answers2

0

The https documentation (https://nodejs.org/api/https.html) provides an example:

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();
Gabriel
  • 580
  • 4
  • 14
  • I checked that, but it didn't worked when Content-Disposition header was "attachment" ... Let me try again and see. – G. Ghez May 02 '15 at 10:15
  • It should still work, the `http` and `https` modules don't give any special attention to the `content-disposition` header. – Gabriel May 03 '15 at 22:40
  • What does it mean Gabriel? I can't download in this case or it should be here when reading response body? – G. Ghez May 09 '15 at 02:19
  • Are you trying to actually download the file? Here's a complete example of how to get the full data into a string variable - https://gist.github.com/gmaybrun/fd35a06e6d0356aceef2. You can do whatever you want with that variable after the `end` handler in `client.js` (for example, write it to a file on the local filesystem). The short version: node.js ignores the `content-disposition` header - it does not automatically save the file for you. It passes the content data to your code to handle like any other response. – Gabriel May 11 '15 at 18:09
0

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

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

finally I got the response using CURL

Mohammed Asad
  • 979
  • 1
  • 8
  • 18