Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds streams example for review #1269

Merged
merged 1 commit into from Nov 15, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions examples/README.md
Expand Up @@ -75,3 +75,41 @@ request.post('https://up.flickr.com/services/upload', {
// assert.equal(typeof body, 'object')
})
```

# Streams

## `POST` data

Use Request as a Writable stream to easily `POST` Readable streams (like files, other HTTP requests, or otherwise).

TL;DR: Pipe a Readable Stream onto Request via:

```
READABLE.pipe(request.post(URL));
```

A more detailed example:

```js
var fs = require('fs')
, path = require('path')
, http = require('http')
, request = require('request')
, TMP_FILE_PATH = path.join(path.sep, 'tmp', 'foo')
;

// write a temporary file:
fs.writeFileSync(TMP_FILE_PATH, 'foo bar baz quk\n');

http.createServer(function(req, res) {
console.log('the server is receiving data!\n');
req
.on('end', res.end.bind(res))
.pipe(process.stdout)
;
}).listen(3000).unref();

fs.createReadStream(TMP_FILE_PATH)
.pipe(request.post('http://127.0.0.1:3000'))
;
```