Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Latest commit

 

History

History
75 lines (53 loc) · 2.28 KB

request.md

File metadata and controls

75 lines (53 loc) · 2.28 KB

request

This modules provides 4 methods (get, post, delete, and put) to simplify sending http requests. Each of these methods returns a promise that resolves with the response.

  • request
    • get
    • post
    • delete
    • put

Making Requests

Making requests is similar to using the Fetch API.

A GET request,

const json = await request('http://www.example.com').then(response => response.json());

A POST request,

const response = await request.post('http://www.example.com', { body: JSON.stringify(myValues)}).then(response => response.json());

Observables

Several observables are available to provide deeper insight into the state of a request.

Monitoring Upload Progress

Upload progress can be monitored with the upload observable on the Request object. Since upload events automatically cause a preflight request, they can be disabled by setting includeUploadProgress: false.

const req = request.post('http://www.example.com/', {
	body: someLargeString
});

req.upload.subscribe(totalUploadedBytes => {
	// do something with uploaded bytes
})

Note that while the Node.js provider will emit a single upload event when it is done uploading, it cannot emit more granular upload events with string or Buffer body types. To receive more frequent upload events, you can use the bodyStream option to provide a Readable with the body content. Upload events will be emitted as the data is read from the stream.

request.post('http://www.example.com/', {
	bodyStream: fs.createReadStream('some-large-file')
});

Monitoring Download Progress

You can monitor download progress by subscribing to the download observable on the Response object.

request("http://www.example/some-large-file").then(response => {
	response.download.subscribe(totalBytesDownloaded => {
		// do something with totalBytesDownloaded
	});
});

Receiving Raw Data

You can receive the raw data from a response with the data observable. Depending on the provider, the value might be a string, or a Buffer.

request("http://www.example/some-large-file").then(response => {
	response.data.subscribe(chunk => {
		// do something with chunk
	});
});