Last active
June 17, 2017 23:52
-
-
Save gentunian/3fa473317935db664d4ea2870305b1ab to your computer and use it in GitHub Desktop.
how to upload a file from body response?
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
var request = require('request'); | |
var slack_token = process.env.SLACK_TOKEN; | |
var channel = process.env.SLACK_CHANNEL; | |
request.post({ | |
url: 'https://slack.com/api/auth.test', | |
formData: { | |
token: slack_token | |
} | |
}, | |
function(err, httpResponse, slackResBody) { | |
if (err) { | |
console.log('ERROR'); | |
console.log(err); | |
} else { | |
var slack_url = JSON.parse(slackResBody)["url"]; | |
console.log('slack url: ' + slack_url); | |
var url = "https://www.google.com.ar/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png"; | |
request.get({ | |
url: url | |
}, | |
function(err, res, body) { | |
request.post({ | |
url: slack_url + '/api/files.upload', | |
formData: { | |
channels: channel, | |
token: slack_token, | |
// How to upload without creating a file and using the body instead? | |
// | |
// The following will work: | |
// var fs = require('fs'); | |
// fs.writeFileSync 'my-file.png' , body; | |
// var readStream = s.createReadStream(tempFile); | |
// | |
file: body // using 'readStream' here will work | |
} | |
}, | |
function(err, res, body) { | |
if (err) { | |
console.log('ERROR'); | |
console.log(err); | |
} else { | |
console.log(res); | |
} | |
}) | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment