blob: 70542d4158876c951c0fa2bc9687d1054f63ef42 (
plain) (
blame)
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
|
'use strict';
module.exports = Response;
/**
* A response from a web request
*
* @param {Number} statusCode
* @param {Object} headers
* @param {Buffer} body
* @param {String} url
*/
function Response(statusCode, headers, body, url) {
if (typeof statusCode !== 'number') {
throw new TypeError('statusCode must be a number but was ' + (typeof statusCode));
}
if (headers === null) {
throw new TypeError('headers cannot be null');
}
if (typeof headers !== 'object') {
throw new TypeError('headers must be an object but was ' + (typeof headers));
}
this.statusCode = statusCode;
this.headers = {};
for (var key in headers) {
this.headers[key.toLowerCase()] = headers[key];
}
this.body = body;
this.url = url;
}
Response.prototype.getBody = function (encoding) {
if (this.statusCode >= 300) {
var err = new Error('Server responded with status code '
+ this.statusCode + ':\n' + this.body.toString());
err.statusCode = this.statusCode;
err.headers = this.headers;
err.body = this.body;
err.url = this.url;
throw err;
}
return encoding ? this.body.toString(encoding) : this.body;
};
|