2

Is it possible to stream and post stdout to formData ?
This code seems to be work but not

const shell = require('shelljs')
const request = require('request')
const isStream = require('isstream')

const file = shell.exec('cat file.jpg', { silent: true, async: true })

console.log(isStream(file.stdout)) // true

const response = request
  .post({
    url,
    formData: {
    image: file.stdout
  }
}, (err, httpResponse, body) => {
    console.log(err)
})

I also tried file.stdout.pipe(PassThrough()) but not worked too

PS:

I just do this with:

cat image.jpg | curl -XPOST https://domain/process --form user_id=123456 --form image=@-

Is it possible to implement above with node ?

PS: I know there is fs.createReadStream But in real project I receive image from an external app stream file on stdout

1 Answer 1

2

I will answer my own question

const spawn = require('child_process').spawn
const ch = spawn('cat', ['./file.jpg'])

let buf = new Buffer(0)

ch.stdout
.on('data', d => {
  buf = Buffer.concat([buf, d])
})
.on('end', () => {
  send(buf)
})

function send (data) {

  request
    .post({
      url,
      formData: {
        image: {
            value: data,
            options: {
                filename: 'test.jpg'
            }
        }
      }
    }, (err, httpResponse, body) => {})
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.