Last active
August 29, 2015 14:19
-
-
Save randavidovitz/0db8fb10121f2cc02964 to your computer and use it in GitHub Desktop.
The correct way to save a file using request module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SaveThePDF(url, filename) { | |
const writeFile = denodeify(fs.writeFile); | |
const requestPromised = denodeify(request.defaults({ | |
'gzip': true, | |
headers: { | |
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36', | |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*\/*;q=0.8', | |
} | |
}), function(err, httpResponse, body) { | |
// Convert 3 paramters to 2 | |
return [err, {httpResponse: httpResponse, body: body}]; | |
}); | |
return requestPromised({ | |
url: url, | |
method: 'post', | |
encoding: 'binary' | |
}).then ( (response) => { | |
return writeFile(filename, response.body, 'ascii'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment