Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Feature Request: Overload http.request to accept a string that is parsed by url.parse #2859

Closed
wants to merge 5 commits into from

2 participants

@adammw

Mostly just for convenience. Example invocation would be:

http.get("http://www.example.com/", function(stream){ ... });
lib/http.js
@@ -1424,6 +1425,7 @@ ClientRequest.prototype.pause = function() {
exports.request = function(options, cb) {
+ if (typeof options == 'string') options = URLParse(options);

Use url.parse() here, makes it obvious that it's an external function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bnoordhuis

Can you add one or two tests?

@adammw

Removed URLParse and replaced with url.parse as per comments, however I'm not sure if requiring from within the function is best practice. Also added a rudimentary test based around how the others function, but again, not sure if it's done in the correct way or what cases has to be tested.

@bnoordhuis

however I'm not sure if requiring from within the function is best practice

It is not. :-) Can you move it to the module prolog? Thanks.

@bnoordhuis

@adammw Do you still want to get this merged?

@adammw

@bnoordhuis I think it would be handy to be merged but I'd want just to check these things first:
I wasn't sure about moving url to prologue as the url variable is used in a different context inside some of the functions in http.js but it seems all of them explicitly declare url first so there shouldn't be a conflict.
Also, I am wondering if the test I created should be removed and substituted with a modified version of the test-http-url.* tests, as they test similar functionality.
Is there any documentation that would also need to be modified in this pull request?
Thanks, for checking up, I had forgotten about this patch...

@bnoordhuis

Also, I am wondering if the test I created should be removed and substituted with a modified version of the test-http-url.* tests, as they test similar functionality.

No need, the test looks fine to me.

Is there any documentation that would also need to be modified in this pull request?

Yes, please. Can you add a small blurb to doc/api/http.markdown?

@bnoordhuis

Adam, can you update the docs? I would like to land this patch.

@adammw

Docs updated in c413c2b (6e0a8bb is optional, but I think suits the example)

@bnoordhuis

Thanks, merged with some modifications (the test wasn't working) in 4099d1e.

@bnoordhuis bnoordhuis closed this
@koichik koichik referenced this pull request from a commit in koichik/node
@koichik koichik https: make https.get() accept a URL
https.get() now accepts either a URL (as a string) or an options object.
Refs #2859.
Fixes #3882.
853718d
@koichik koichik referenced this pull request from a commit
@koichik koichik https: make https.get() accept a URL
https.get() now accepts either a URL (as a string) or an options object.

Refs #2859.
Fixes #3882.
752ac32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
13 doc/api/http.markdown
@@ -425,8 +425,9 @@ followed by `response.end()`.
## http.request(options, callback)
Node maintains several connections per server to make HTTP requests.
-This function allows one to transparently issue requests. `options` align
-with [url.parse()](url.html#url.parse).
+This function allows one to transparently issue requests.
+
+`options` can be an object or a string. If `options` is a string, it is automatically parsed with [url.parse()](url.html#url.parse).
Options:
@@ -511,13 +512,7 @@ that it sets the method to GET and calls `req.end()` automatically.
Example:
- var options = {
- host: 'www.google.com',
- port: 80,
- path: '/index.html'
- };
-
- http.get(options, function(res) {
+ http.get("http://www.google.com/index.html", function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
View
4 lib/http.js
@@ -22,6 +22,7 @@
var util = require('util');
var net = require('net');
var stream = require('stream');
+var url = require('url');
var EventEmitter = require('events').EventEmitter;
var FreeList = require('freelist').FreeList;
var HTTPParser = process.binding('http_parser').HTTPParser;
@@ -1424,6 +1425,9 @@ ClientRequest.prototype.pause = function() {
exports.request = function(options, cb) {
+ if (typeof options == 'string') {
+ options = url.parse(options);
+ }
if (options.protocol && options.protocol !== 'http:') {
throw new Error('Protocol:' + options.protocol + ' not supported.');
}
View
57 test/simple/test-http-client-get-url.js
@@ -0,0 +1,57 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common');
+var assert = require('assert');
+var http = require('http');
+
+var sent_body = '';
+var server_req_complete = false;
+var client_res_complete = false;
+
+var server = http.createServer(function(req, res) {
+ assert.equal('GET', req.method);
+ assert.equal('/foo?bar', req.url);
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.write('hello\n');
+ res.end();
+});
+server.listen(common.PORT);
+
+server.on('listening', function() {
+ var req = http.get("http://127.0.0.1/foo?bar", function(res) {
+ res.setEncoding('utf8');
+ res.on('data', function(chunk) {
+ console.log(chunk);
+ });
+ res.on('end', function() {
+ server.close();
+ });
+ });
+
+ common.error('client finished sending request');
+});
+
+process.on('exit', function() {
+ assert.equal('1\n2\n3\n', sent_body);
+ assert.equal(true, server_req_complete);
+ assert.equal(true, client_res_complete);
+});
Something went wrong with that request. Please try again.