@@ -6,23 +6,43 @@ responses from external servers.
66
77## Usage
88
9- Basic usage is to require the module and call it, passing in the object
10- returned by ` require('request') ` :
9+ Basic usage is to require the module and call it:
1110
1211``` js
13- var request = require (' request' );
12+ const RequestDebug = require (' request-debug ' );
1413
15- require ( ' request-debug ' )(request );
14+ const client = new RequestDebug ( );
1615```
1716
18- This will set up event handlers on every request performed with the ` request `
17+ This will set up event handlers on every request performed with the ` client `
1918variable from this point.
2019
20+ The constructor ` RequestDebug ` supports all options of ` request ` (see options [ here] ( https://github.com/request/request#requestoptions-callback ) ):
21+ ``` js
22+ const RequestDebug = require (' request-debug' );
23+
24+ const client = new RequestDebug ({
25+ baseUrl: ' http://www.google.com' ,
26+ json: true
27+ });
28+
29+ ```
30+
2131You can also specify a function to handle request or response data:
2232
2333``` js
24- require (' request-debug' )(request, function (type , data , r ) {
34+ const client = new RequestDebug ({
35+ logger : (type , data , r ) => {
2536 // put your request or response handling logic here
37+ }
38+ });
39+ ```
40+ or by method ` logger ` :
41+ ``` js
42+ const client = new RequestDebug ();
43+
44+ request .logger ((type , data , r ) => {
45+ // put your request or response handling logic here
2646});
2747```
2848
@@ -51,82 +71,75 @@ responses and other events.
5171The default handling function writes the data to * stderr* in Node's JSON-like
5272object display format. See the example below for more details.
5373
54- To disable debugging, call ` request.stopDebugging() ` (this function only exists
55- if debugging has already been enabled). Any requests that are in progress when
74+ To disable debugging, call ` request.stopDebugging() ` . Any requests that are in progress when
5675` stopDebugging() ` is called will still generate debug events.
5776
5877## Example
5978
6079``` js
61- var request = require (' request' );
62-
63- require (' request-debug' )(request);
64-
65- // digest.php is example 2 from:
66- // http://php.net/manual/en/features.http-auth.php
80+ const client = new RequestDebug ();
6781
68- request ({
69- uri : ' http://nylen.tv/digest.php' ,
70- auth : {
71- user : ' admin' ,
72- pass : ' mypass' ,
73- sendImmediately : false
74- },
75- rejectUnauthorized : false ,
82+ client .request ({
83+ uri : ' https://raw.githubusercontent.com/request/request-debug/master/.gitignore' ,
7684}, function (err , res , body ) {
77- console .log (' REQUEST RESULTS:' , err, res .statusCode , body );
85+ console .log (' REQUEST RESULTS:' , err, res .statusCode );
7886});
7987```
8088
81- Unless you provide your own function as the second parameter to the
82- ` request-debug ` call, this will produce console output similar to the
83- following:
89+ ` request-debug ` also supports calling ` delete ` , ` get ` , ` head ` , ` patch ` , ` post ` and ` put ` :
90+
91+ ``` js
92+ const client = new RequestDebug ();
93+
94+ client .get (' https://github.com/request/request-debug' );
95+
96+ client .post (' https://github.com/request/request-debug' );
97+
98+ client .head (' https://github.com/request/request-debug' );
99+ ```
100+
101+ Unless you provide your own logger function in options on the ` request-debug ` constructor call or set it via ` logger ` method, this will produce console output similar to thefollowing:
84102
85103``` js
86104{ request:
87105 { debugId: 1 ,
88- uri: ' http://nylen.tv/digest.php' ,
89- method: ' GET' ,
90- headers: { host: ' nylen.tv' } } }
91- { auth:
92- { debugId: 1 ,
93- statusCode: 401 ,
94- headers:
95- { date: ' Mon, 20 Oct 2014 03:34:58 GMT' ,
96- server: ' Apache/2.4.6 (Debian)' ,
97- ' x-powered-by' : ' PHP/5.5.6-1' ,
98- ' www-authenticate' : ' Digest realm="Restricted area",qop="auth",nonce="544482e2556d9",opaque="cdce8a5c95a1427d74df7acbf41c9ce0"' ,
99- ' content-length' : ' 39' ,
100- ' keep-alive' : ' timeout=5, max=100' ,
101- connection: ' Keep-Alive' ,
102- ' content-type' : ' text/html' },
103- uri: ' http://nylen.tv/digest.php' } }
104- { request:
105- { debugId: 1 ,
106- uri: ' http://nylen.tv/digest.php' ,
106+ headers: { host: ' raw.githubusercontent.com' },
107107 method: ' GET' ,
108- headers :
109- { authorization : ' Digest username="admin", realm="Restricted area", nonce="544482e2556d9", uri="/digest.php", qop=auth, response="e833c7fa52e8d42fae3ca784b96dfd38", nc=00000001, cnonce="ab6ff3dd95a0449e990a6c8465a6bb26", opaque="cdce8a5c95a1427d74df7acbf41c9ce0" ' ,
110- host : ' nylen.tv ' } } }
108+ uri : ' https://raw.githubusercontent.com/request/request-debug/master/.gitignore ' } }
109+ REQUEST RESULTS : null 200 node_modules
110+
111111{ response:
112- { debugId: 1 ,
112+ { body: ' node_modules\n ' ,
113+ debugId: 1 ,
113114 headers:
114- { date: ' Mon, 20 Oct 2014 03:34:58 GMT' ,
115- server: ' Apache/2.4.6 (Debian)' ,
116- ' x-powered-by' : ' PHP/5.5.6-1' ,
117- ' content-length' : ' 27' ,
118- ' keep-alive' : ' timeout=5, max=100' ,
119- connection: ' Keep-Alive' ,
120- ' content-type' : ' text/html' },
121- statusCode: 200 ,
122- body: ' You are logged in as: admin' } }
123- REQUEST RESULTS : null 200 You are logged in as: admin
115+ { ' content-security-policy' : ' default-src \' none\' ; style-src \' unsafe-inline\' ' ,
116+ ' strict-transport-security' : ' max-age=31536000' ,
117+ ' x-content-type-options' : ' nosniff' ,
118+ ' x-frame-options' : ' deny' ,
119+ ' x-xss-protection' : ' 1; mode=block' ,
120+ etag: ' "3c3629e647f5ddf82548912e337bea9826b434af"' ,
121+ ' content-type' : ' text/plain; charset=utf-8' ,
122+ ' cache-control' : ' max-age=300' ,
123+ ' x-geo-block-list' : ' ' ,
124+ ' x-github-request-id' : ' B91F1215:5247:1A4348B:57E4F4E1' ,
125+ ' content-length' : ' 13' ,
126+ ' accept-ranges' : ' bytes' ,
127+ date: ' Fri, 23 Sep 2016 09:25:24 GMT' ,
128+ via: ' 1.1 varnish' ,
129+ connection: ' close' ,
130+ ' x-served-by' : ' cache-lcy1123-LCY' ,
131+ ' x-cache' : ' HIT' ,
132+ ' x-cache-hits' : ' 2' ,
133+ vary: ' Authorization,Accept-Encoding' ,
134+ ' access-control-allow-origin' : ' *' ,
135+ ' x-fastly-request-id' : ' 6c0b96eef9098ba115b0f874a4ec48e9086b0669' ,
136+ expires: ' Fri, 23 Sep 2016 09:30:24 GMT' ,
137+ ' source-age' : ' 35' },
138+ method: ' GET' ,
139+ statusCode: 200 } }
124140```
125141
126142## Compatibility
127143
128- Tested with Node.js versions 0.8. x, 0.10. x, and 0.11 .x on Travis, and a bunch
144+ Tested with Node.js versions 4.0. x, 5.0. x, 6.0.x and 6.6 .x on Travis, and a bunch
129145of different ` request ` versions.
130-
131- Does not work with ` request ` versions older than 2.22.0 (July 2013). Tests
132- don't start passing until version 2.28.0 (December 2013).
0 commit comments