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

Commit

Permalink
fix: use const instead of var in the README (request#3046)
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey authored and reconbot committed Nov 24, 2018
1 parent 5ee8906 commit 81b171b
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions README.md
Expand Up @@ -16,7 +16,7 @@
Request is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default.

```js
var request = require('request');
const request = require('request');
request('http://www.google.com', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
Expand Down Expand Up @@ -110,7 +110,7 @@ You can also `pipe()` from `http.ServerRequest` instances, as well as to `http.S
```js
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
var x = request('http://mysite.com/doodle.png')
const x = request('http://mysite.com/doodle.png')
req.pipe(x)
x.pipe(resp)
}
Expand All @@ -126,7 +126,7 @@ req.pipe(request('http://mysite.com/doodle.png')).pipe(resp)
Also, none of this new functionality conflicts with requests previous features, it just expands them.

```js
var r = request.defaults({'proxy':'http://localproxy.com'})
const r = request.defaults({'proxy':'http://localproxy.com'})

http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
Expand Down Expand Up @@ -185,7 +185,7 @@ For `multipart/form-data` we use the [form-data](https://github.com/form-data/fo


```js
var formData = {
const formData = {
// Pass a simple key-value pair
my_field: 'my_value',
// Pass data via Buffers
Expand Down Expand Up @@ -220,8 +220,8 @@ For advanced cases, you can access the form-data object itself via `r.form()`. T

```js
// NOTE: Advanced use-case, for normal use see 'formData' usage above
var r = request.post('http://service.com/upload', function optionalCallback(err, httpResponse, body) {...})
var form = r.form();
const r = request.post('http://service.com/upload', function optionalCallback(err, httpResponse, body) {...})
const form = r.form();
form.append('my_field', 'my_value');
form.append('my_buffer', Buffer.from([1, 2, 3]));
form.append('custom_file', fs.createReadStream(__dirname + '/unicycle.jpg'), {filename: 'unicycle.jpg'});
Expand Down Expand Up @@ -316,11 +316,11 @@ detailed in [RFC 1738](http://www.ietf.org/rfc/rfc1738.txt). Simply pass the
`user:password` before the host with an `@` sign:

```js
var username = 'username',
const username = 'username',
password = 'password',
url = 'http://' + username + ':' + password + '@some.server.com';

request({url: url}, function (error, response, body) {
request({url}, function (error, response, body) {
// Do more stuff with 'body' here
});
```
Expand Down Expand Up @@ -349,9 +349,9 @@ of stars and forks for the request repository. This requires a
custom `User-Agent` header as well as https.

```js
var request = require('request');
const request = require('request');

var options = {
const options = {
url: 'https://api.github.com/repos/request/request',
headers: {
'User-Agent': 'request'
Expand All @@ -360,7 +360,7 @@ var options = {

function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
const info = JSON.parse(body);
console.log(info.stargazers_count + " Stars");
console.log(info.forks_count + " Forks");
}
Expand All @@ -384,7 +384,7 @@ default signing algorithm is
```js
// OAuth1.0 - 3-legged server side flow (Twitter example)
// step 1
var qs = require('querystring')
const qs = require('querystring')
, oauth =
{ callback: 'http://mysite.com/callback/'
, consumer_key: CONSUMER_KEY
Expand All @@ -399,14 +399,14 @@ request.post({url:url, oauth:oauth}, function (e, r, body) {
// verified with twitter that they are authorizing your app.

// step 2
var req_data = qs.parse(body)
var uri = 'https://api.twitter.com/oauth/authenticate'
const req_data = qs.parse(body)
const uri = 'https://api.twitter.com/oauth/authenticate'
+ '?' + qs.stringify({oauth_token: req_data.oauth_token})
// redirect the user to the authorize uri

// step 3
// after the user is redirected back to your server
var auth_data = qs.parse(body)
const auth_data = qs.parse(body)
, oauth =
{ consumer_key: CONSUMER_KEY
, consumer_secret: CONSUMER_SECRET
Expand All @@ -418,7 +418,7 @@ request.post({url:url, oauth:oauth}, function (e, r, body) {
;
request.post({url:url, oauth:oauth}, function (e, r, body) {
// ready to make signed requests on behalf of the user
var perm_data = qs.parse(body)
const perm_data = qs.parse(body)
, oauth =
{ consumer_key: CONSUMER_KEY
, consumer_secret: CONSUMER_SECRET
Expand Down Expand Up @@ -607,14 +607,14 @@ TLS/SSL Protocol options, such as `cert`, `key` and `passphrase`, can be
set directly in `options` object, in the `agentOptions` property of the `options` object, or even in `https.globalAgent.options`. Keep in mind that, although `agentOptions` allows for a slightly wider range of configurations, the recommended way is via `options` object directly, as using `agentOptions` or `https.globalAgent.options` would not be applied in the same way in proxied environments (as data travels through a TLS connection instead of an http/https agent).

```js
var fs = require('fs')
const fs = require('fs')
, path = require('path')
, certFile = path.resolve(__dirname, 'ssl/client.crt')
, keyFile = path.resolve(__dirname, 'ssl/client.key')
, caFile = path.resolve(__dirname, 'ssl/ca.cert.pem')
, request = require('request');

var options = {
const options = {
url: 'https://api.some-server.com/',
cert: fs.readFileSync(certFile),
key: fs.readFileSync(keyFile),
Expand All @@ -631,13 +631,13 @@ In the example below, we call an API that requires client side SSL certificate
(in PEM format) with passphrase protected private key (in PEM format) and disable the SSLv3 protocol:

```js
var fs = require('fs')
const fs = require('fs')
, path = require('path')
, certFile = path.resolve(__dirname, 'ssl/client.crt')
, keyFile = path.resolve(__dirname, 'ssl/client.key')
, request = require('request');

var options = {
const options = {
url: 'https://api.some-server.com/',
agentOptions: {
cert: fs.readFileSync(certFile),
Expand Down Expand Up @@ -708,7 +708,7 @@ The `options.har` property will override the values: `url`, `method`, `qs`, `hea
A validation step will check if the HAR Request format matches the latest spec (v1.2) and will skip parsing if not matching.

```js
var request = require('request')
const request = require('request')
request({
// will be ignored
method: 'GET',
Expand Down Expand Up @@ -902,13 +902,13 @@ instead, it **returns a wrapper** that has your default settings applied to it.
For example:
```js
//requests using baseRequest() will set the 'x-token' header
var baseRequest = request.defaults({
const baseRequest = request.defaults({
headers: {'x-token': 'my-token'}
})

//requests using specialRequest() will include the 'x-token' header set in
//baseRequest and will also include the 'special' header
var specialRequest = baseRequest.defaults({
const specialRequest = baseRequest.defaults({
headers: {special: 'special value'}
})
```
Expand Down Expand Up @@ -1008,7 +1008,7 @@ request.get('http://10.255.255.1', {timeout: 1500}, function(err) {
## Examples:

```js
var request = require('request')
const request = require('request')
, rand = Math.floor(Math.random()*100000000).toString()
;
request(
Expand Down Expand Up @@ -1039,7 +1039,7 @@ while the response object is unmodified and will contain compressed data if
the server sent a compressed response.

```js
var request = require('request')
const request = require('request')
request(
{ method: 'GET'
, uri: 'http://www.google.com'
Expand Down Expand Up @@ -1067,7 +1067,7 @@ the server sent a compressed response.
Cookies are disabled by default (else, they would be used in subsequent requests). To enable cookies, set `jar` to `true` (either in `defaults` or `options`).

```js
var request = request.defaults({jar: true})
const request = request.defaults({jar: true})
request('http://www.google.com', function () {
request('http://images.google.com')
})
Expand All @@ -1076,8 +1076,8 @@ request('http://www.google.com', function () {
To use a custom cookie jar (instead of `request`’s global cookie jar), set `jar` to an instance of `request.jar()` (either in `defaults` or `options`)

```js
var j = request.jar()
var request = request.defaults({jar:j})
const j = request.jar()
const request = request.defaults({jar:j})
request('http://www.google.com', function () {
request('http://images.google.com')
})
Expand All @@ -1086,9 +1086,9 @@ request('http://www.google.com', function () {
OR

```js
var j = request.jar();
var cookie = request.cookie('key1=value1');
var url = 'http://www.google.com';
const j = request.jar();
const cookie = request.cookie('key1=value1');
const url = 'http://www.google.com';
j.setCookie(cookie, url);
request({url: url, jar: j}, function () {
request('http://images.google.com')
Expand All @@ -1101,9 +1101,9 @@ which supports saving to and restoring from JSON files), pass it as a parameter
to `request.jar()`:

```js
var FileCookieStore = require('tough-cookie-filestore');
const FileCookieStore = require('tough-cookie-filestore');
// NOTE - currently the 'cookies.json' file must already exist!
var j = request.jar(new FileCookieStore('cookies.json'));
const j = request.jar(new FileCookieStore('cookies.json'));
request = request.defaults({ jar : j })
request('http://www.google.com', function() {
request('http://images.google.com')
Expand All @@ -1119,10 +1119,10 @@ for details.
To inspect your cookie jar after a request:

```js
var j = request.jar()
const j = request.jar()
request({url: 'http://www.google.com', jar: j}, function () {
var cookie_string = j.getCookieString(url); // "key1=value1; key2=value2; ..."
var cookies = j.getCookies(url);
const cookie_string = j.getCookieString(url); // "key1=value1; key2=value2; ..."
const cookies = j.getCookies(url);
// [{key: 'key1', value: 'value1', domain: "www.google.com", ...}, ...]
})
```
Expand Down

0 comments on commit 81b171b

Please sign in to comment.