Skip to content

Commit da34535

Browse files
committed
Merge pull request request#1269 from tbuchok/post-stream-example
adds streams example for review
2 parents 8e3aa3b + 9bd58ad commit da34535

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

examples/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,41 @@ request.post('https://up.flickr.com/services/upload', {
7575
// assert.equal(typeof body, 'object')
7676
})
7777
```
78+
79+
# Streams
80+
81+
## `POST` data
82+
83+
Use Request as a Writable stream to easily `POST` Readable streams (like files, other HTTP requests, or otherwise).
84+
85+
TL;DR: Pipe a Readable Stream onto Request via:
86+
87+
```
88+
READABLE.pipe(request.post(URL));
89+
```
90+
91+
A more detailed example:
92+
93+
```js
94+
var fs = require('fs')
95+
, path = require('path')
96+
, http = require('http')
97+
, request = require('request')
98+
, TMP_FILE_PATH = path.join(path.sep, 'tmp', 'foo')
99+
;
100+
101+
// write a temporary file:
102+
fs.writeFileSync(TMP_FILE_PATH, 'foo bar baz quk\n');
103+
104+
http.createServer(function(req, res) {
105+
console.log('the server is receiving data!\n');
106+
req
107+
.on('end', res.end.bind(res))
108+
.pipe(process.stdout)
109+
;
110+
}).listen(3000).unref();
111+
112+
fs.createReadStream(TMP_FILE_PATH)
113+
.pipe(request.post('http://127.0.0.1:3000'))
114+
;
115+
```

0 commit comments

Comments
 (0)