333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
node-httpreq
============
 
node-httpreq is a node.js library to do HTTP(S) requests the easy way
 
Do GET, POST, PUT, PATCH, DELETE, OPTIONS, upload files, use cookies, change headers, ...
 
## Install
 
You can install __httpreq__ using the Node Package Manager (npm):
 
    npm install httpreq
 
## Simple example
```js
var httpreq = require('httpreq');
 
httpreq.get('http://www.google.com', function (err, res){
  if (err) return console.log(err);
 
  console.log(res.statusCode);
  console.log(res.headers);
  console.log(res.body);
  console.log(res.cookies);
});
```
 
## How to use
 
* [httpreq.get(url, [options], callback)](#get)
* [httpreq.post(url, [options], callback)](#post)
* [httpreq.put(url, [options], callback)](#put)
* [httpreq.delete(url, [options], callback)](#delete)
* [httpreq.options(url, [options], callback)](#options)
* [Uploading files](#upload)
* [Downloading a binary file](#binary)
* [Downloading a file directly to disk](#download)
* [Sending a custom body](#custombody)
* [Using a http(s) proxy](#proxy)
* [httpreq.doRequest(options, callback)](#dorequest)
 
---------------------------------------
### httpreq.get(url, [options], callback)
<a name="get"></a>
 
__Arguments__
 - url: The url to connect to. Can be http or https.
 - options: (all are optional) The following options can be passed:
    - parameters: an object of query parameters
    - headers: an object of headers
    - cookies: an array of cookies
    - auth: a string for basic authentication. For example `username:password`
    - binary: true/false (default: false), if true, res.body will a buffer containing the binary data
    - allowRedirects: (default: __true__ , only with httpreq.get() ), if true, redirects will be followed
    - maxRedirects: (default: __10__ ). For example 1 redirect will allow for one normal request and 1 extra redirected request.
    - timeout: (default: __none__ ). Adds a timeout to the http(s) request. Should be in milliseconds.
    - proxy, if you want to pass your request through a http(s) proxy server:
        - host: eg: "192.168.0.1"
        - port: eg: 8888
        - protocol: (default: __'http'__ ) can be 'http' or 'https'
     - rejectUnauthorized: validate certificate for request with HTTPS. [More here](http://nodejs.org/api/https.html#https_https_request_options_callback)
 - callback(err, res): A callback function which is called when the request is complete. __res__ contains the headers ( __res.headers__ ), the http status code ( __res.statusCode__ ) and the body ( __res.body__ )
 
__Example without options__
 
```js
var httpreq = require('httpreq');
 
httpreq.get('http://www.google.com', function (err, res){
  if (err) return console.log(err);
 
  console.log(res.statusCode);
  console.log(res.headers);
  console.log(res.body);
});
```
 
__Example with options__
 
```js
var httpreq = require('httpreq');
 
httpreq.get('http://posttestserver.com/post.php', {
  parameters: {
    name: 'John',
    lastname: 'Doe'
  },
  headers:{
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:18.0) Gecko/20100101 Firefox/18.0'
  },
  cookies: [
    'token=DGcGUmplWQSjfqEvmu%2BZA%2Fc',
    'id=2'
  ]
}, function (err, res){
  if (err){
    console.log(err);
  }else{
    console.log(res.body);
  }
});
```
---------------------------------------
### httpreq.post(url, [options], callback)
<a name="post"></a>
 
__Arguments__
 - url: The url to connect to. Can be http or https.
 - options: (all are optional) The following options can be passed:
    - parameters: an object of post parameters (content-type is set to *application/x-www-form-urlencoded; charset=UTF-8*)
    - json: if you want to send json directly (content-type is set to *application/json*)
    - files: an object of files to upload (content-type is set to *multipart/form-data; boundary=xxx*)
    - body: custom body content you want to send. If used, previous options will be ignored and your custom body will be sent. (content-type will not be set)
    - headers: an object of headers
    - cookies: an array of cookies
    - auth: a string for basic authentication. For example `username:password`
    - binary: true/false (default: __false__ ), if true, res.body will be a buffer containing the binary data
    - allowRedirects: (default: __false__ ), if true, redirects will be followed
    - maxRedirects: (default: __10__ ). For example 1 redirect will allow for one normal request and 1 extra redirected request.
    - encodePostParameters: (default: __true__ ), if true, POST/PUT parameters names will be URL encoded.
    - timeout: (default: none). Adds a timeout to the http(s) request. Should be in milliseconds.
    - proxy, if you want to pass your request through a http(s) proxy server:
        - host: eg: "192.168.0.1"
        - port: eg: 8888
        - protocol: (default: __'http'__ ) can be 'http' or 'https'
    - rejectUnauthorized: validate certificate for request with HTTPS. [More here](http://nodejs.org/api/https.html#https_https_request_options_callback)
 - callback(err, res): A callback function which is called when the request is complete. __res__ contains the headers ( __res.headers__ ), the http status code ( __res.statusCode__ ) and the body ( __res.body__ )
 
__Example without extra options__
 
```js
var httpreq = require('httpreq');
 
httpreq.post('http://posttestserver.com/post.php', {
  parameters: {
    name: 'John',
    lastname: 'Doe'
  }
}, function (err, res){
  if (err){
    console.log(err);
  }else{
    console.log(res.body);
  }
});
```
 
__Example with options__
 
```js
var httpreq = require('httpreq');
 
httpreq.post('http://posttestserver.com/post.php', {
  parameters: {
    name: 'John',
    lastname: 'Doe'
  },
  headers:{
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:18.0) Gecko/20100101 Firefox/18.0'
  },
  cookies: [
    'token=DGcGUmplWQSjfqEvmu%2BZA%2Fc',
    'id=2'
  ]
}, function (err, res){
  if (err){
    console.log(err);
  }else{
    console.log(res.body);
  }
});
```
 
---------------------------------------
### httpreq.put(url, [options], callback)
<a name="put"></a>
 
Same options as [httpreq.post(url, [options], callback)](#post)
 
---------------------------------------
<a name="delete" />
### httpreq.delete(url, [options], callback)
 
Same options as [httpreq.post(url, [options], callback)](#post)
 
---------------------------------------
<a name="options" />
### httpreq.options(url, [options], callback)
 
Same options as [httpreq.get(url, [options], callback)](#get) except for the ability to follow redirects.
 
---------------------------------------
<a name="upload" />
### Uploading files
 
You can still use ```httpreq.uploadFiles({url: 'url', files: {}}, callback)```, but it's easier to just use POST (or PUT):
 
__Example__
 
```js
var httpreq = require('httpreq');
 
httpreq.post('http://posttestserver.com/upload.php', {
  parameters: {
    name: 'John',
    lastname: 'Doe'
  },
  files:{
    myfile: __dirname + "/testupload.jpg",
    myotherfile: __dirname + "/testupload.jpg"
  }
}, function (err, res){
  if (err) throw err;
});
```
 
---------------------------------------
<a name="binary"></a>
### Downloading a binary file
To download a binary file, just add __binary: true__ to the options when doing a get or a post.
 
__Example__
 
```js
var httpreq = require('httpreq');
 
httpreq.get('https://ssl.gstatic.com/gb/images/k1_a31af7ac.png', {binary: true}, function (err, res){
  if (err){
    console.log(err);
  }else{
    fs.writeFile(__dirname + '/test.png', res.body, function (err) {
      if(err)
        console.log("error writing file");
    });
  }
});
```
 
---------------------------------------
<a name="download"></a>
### Downloading a file directly to disk
To download a file directly to disk, use the download method provided.
 
Downloading is done using a stream, so the data is not stored in memory and directly saved to file.
 
__Example__
 
```js
var httpreq = require('httpreq');
 
httpreq.download(
  'https://ssl.gstatic.com/gb/images/k1_a31af7ac.png',
  __dirname + '/test.png'
, function (err, progress){
  if (err) return console.log(err);
  console.log(progress);
}, function (err, res){
  if (err) return console.log(err);
  console.log(res);
});
 
```
---------------------------------------
<a name="custombody"></a>
### Sending a custom body
Use the body option to send a custom body (eg. an xml post)
 
__Example__
 
```js
var httpreq = require('httpreq');
 
httpreq.post('http://posttestserver.com/post.php',{
  body: '<?xml version="1.0" encoding="UTF-8"?>',
  headers:{
    'Content-Type': 'text/xml',
  }},
  function (err, res) {
    if (err){
      console.log(err);
    }else{
      console.log(res.body);
    }
  }
);
```
 
---------------------------------------
<a name="proxy"></a>
### Using a http(s) proxy
 
__Example__
 
```js
var httpreq = require('httpreq');
 
httpreq.post('http://posttestserver.com/post.php', {
  proxy: {
    host: '10.100.0.126',
    port: 8888
  }
}, function (err, res){
  if (err){
    console.log(err);
  }else{
    console.log(res.body);
  }
});
```
 
---------------------------------------
### httpreq.doRequest(options, callback)
<a name="dorequest"></a>
 
httpreq.doRequest is internally used by httpreq.get() and httpreq.post(). You can use this directly. Everything is stays the same as httpreq.get() or httpreq.post() except that the following options MUST be passed:
- url: the url to post the files to
- method: 'GET', 'POST', 'PUT' or 'DELETE'
 
## Donate
 
If you like this module or you want me to update it faster, feel free to donate. It helps increasing my dedication to fixing bugs :-)
 
[![](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=AB3R2SUL53K7S)