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 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

i wonder why this works, and what is the issue with createReadStream
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

1 Comment

I got a { [Error: write EPIPE] code: 'EPIPE', errno: 'EPIPE', syscall: 'write' } error.
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);
        })
    )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.