3

I would like to pass a readable stream to the post request body using pipe, but am having trouble. This is the code I have:

var request = require('request');  
var fs = require('fs');   
var source = fs.createReadStream('./originalJsonDataWithObject.json');  //creating a read stream to read the file 
    source.pipe(request.post('http://localhost:3030/decompress'));  //piping it to the post request
1

1 Answer 1

1
var request = require('request');
var fs = require('fs');
var file = fs.createReadStream('./originalJsonDataWithObject.json');
var req = request.post({
  url: 'your post url',
  headers: {<headers>},
  body: file
});

POST request body parameter is the data you are actually sending with your request. This data can be in many forms (stream, buffer, string etc.) You do not need to pipe it. If you need to post JSON data, you can do this:

    const req = request.post({
        url: 'http://localhost:3030/decompress',  
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(<your JSON data>)                
    });
Sign up to request clarification or add additional context in comments.

1 Comment

included, sorry about that.

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.