dominictarr / JSONStream

rawStream.pipe(JSONStream.parse()).pipe(streamOfObjects)
Other
1.91k stars 165 forks source link

Is it possible to use JSONStream to read the request of a POST method in a REST API? #158

Open alejandrofdez-us opened 6 years ago

alejandrofdez-us commented 6 years ago

Any sample of this? Thanks in advance!

sim2github commented 6 years ago

You can pipe request to parser and use event data

JSONStream.parse([true])
    .pipe(
        request(
            {
                method: 'POST',
                url: this.url.href + '/_bulk_docs',
                headers: this.headers
            },
            function (err, res, body) {
                if (err) {
                    console.log('RESPONSE ERROR:', err)
                    return
                }
                if (res && res.statusCode !== 201) {
                    console.log(res.statusMessage, res.body)
                }
            })
    )
    .on('error', err => {
        console.error('INPUT ERROR:', err)
    })
    .on('data', doc => {
        console.log(doc.toString())
        // or write to other stream e.g. JSONStream.stringify()
    })
    .on('end', () => {
         // Event on end
    })