2

What I am trying to do is to translate this curl command into Node.js code

curl -X PUT -H "Content-Type:" --data-binary @slug.tgz "https://s3-external-1.amazonaws.com/herokuslugs/heroku.com/v1/xyz"

I have been fiddling with Node's built-in https.request and the request lib for more than a hour and still haven't figured that out.

3 Answers3

5

I ran into this problem earlier. I got it work by changing from fs.createReadStream to fs.readFile. Here's a quick sample of what I did.

fs.readFile('@slug.tgz', function (err, data) {
  if (err) return console.error(err);

  options = {
    url: 'https://s3-external-1.amazonaws.com/herokuslugs/heroku.com/v1/xyz',
    body: data
  }

  request.put(options, function (err, message, data) {
    if (err) return console.error(err);

    console.log(data)
  });
});

When I used fs.createReadStream I got an error from Amazon. Also make sure you first create a slug through the platfrom API and use the url from the blog in the response.

chollida
  • 7,834
  • 11
  • 55
  • 85
agchou
  • 737
  • 6
  • 8
0
curl -X PUT -H "Content-Type:" --data-binary @slug.tgz "https://s3-external-1.amazonaws.com/herokuslugs/heroku.com/v1/xyz"

i'm not positive this works but this is what I would try first (the third example in the request docs):

fs.createReadStream('@slug.tgz').pipe(request.put('https://s3-external-1.amazonaws.com/herokuslugs/heroku.com/v1/xyz'))

you might also want to use amazon's official node library as I demonstrate in this answer

Community
  • 1
  • 1
Plato
  • 10,812
  • 2
  • 41
  • 61
  • I got a `{ [Error: write EPIPE] code: 'EPIPE', errno: 'EPIPE', syscall: 'write' }` error. –  Jan 29 '14 at 22:01
0

following code works for me

var options = {
        url: <url>,
        method: "PUT",
    };

    fs.createReadStream(file_path).pipe(
        request(options, (err, response) => {
            console.log(response.statusCode);
            var body = JSON.parse(response.body);
        })
    )
Yogesh
  • 865
  • 6
  • 13