Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 4099d1e

Browse files
adammwbnoordhuis
authored andcommitted
http: make http.get() accept a URL
http.get() now accepts either a URL (as a string) or an options object.
1 parent 05b81f3 commit 4099d1e

3 files changed

Lines changed: 56 additions & 11 deletions

File tree

doc/api/http.markdown

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,10 @@ followed by `response.end()`.
446446
## http.request(options, callback)
447447

448448
Node maintains several connections per server to make HTTP requests.
449-
This function allows one to transparently issue requests. `options` align
450-
with [url.parse()](url.html#url.parse).
449+
This function allows one to transparently issue requests.
450+
451+
`options` can be an object or a string. If `options` is a string, it is
452+
automatically parsed with [url.parse()](url.html#url.parse).
451453

452454
Options:
453455

@@ -528,18 +530,12 @@ There are a few special headers that should be noted.
528530
## http.get(options, callback)
529531

530532
Since most requests are GET requests without bodies, Node provides this
531-
convenience method. The only difference between this method and `http.request()` is
532-
that it sets the method to GET and calls `req.end()` automatically.
533+
convenience method. The only difference between this method and `http.request()`
534+
is that it sets the method to GET and calls `req.end()` automatically.
533535

534536
Example:
535537

536-
var options = {
537-
host: 'www.google.com',
538-
port: 80,
539-
path: '/index.html'
540-
};
541-
542-
http.get(options, function(res) {
538+
http.get("http://www.google.com/index.html", function(res) {
543539
console.log("Got response: " + res.statusCode);
544540
}).on('error', function(e) {
545541
console.log("Got error: " + e.message);

lib/http.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
var util = require('util');
2323
var net = require('net');
2424
var stream = require('stream');
25+
var url = require('url');
2526
var EventEmitter = require('events').EventEmitter;
2627
var FreeList = require('freelist').FreeList;
2728
var HTTPParser = process.binding('http_parser').HTTPParser;
@@ -1571,6 +1572,10 @@ ClientRequest.prototype.clearTimeout = function(cb) {
15711572
};
15721573

15731574
exports.request = function(options, cb) {
1575+
if (typeof options === 'string') {
1576+
options = url.parse(options);
1577+
}
1578+
15741579
if (options.protocol && options.protocol !== 'http:') {
15751580
throw new Error('Protocol:' + options.protocol + ' not supported.');
15761581
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var http = require('http');
25+
26+
var seen_req = false;
27+
28+
var server = http.createServer(function(req, res) {
29+
assert.equal('GET', req.method);
30+
assert.equal('/foo?bar', req.url);
31+
res.writeHead(200, {'Content-Type': 'text/plain'});
32+
res.write('hello\n');
33+
res.end();
34+
server.close();
35+
seen_req = true;
36+
});
37+
38+
server.listen(common.PORT, function() {
39+
http.get('http://127.0.0.1:' + common.PORT + '/foo?bar');
40+
});
41+
42+
process.on('exit', function() {
43+
assert(seen_req);
44+
});

0 commit comments

Comments
 (0)