Created
February 5, 2014 16:45
-
-
Save MorganConrad/8827916 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Request = require('request/request.js'); // IMPORTANT - specify request.js, don't get index.js!!! | |
var querystring = require('querystring'); | |
MyRequest.prototype = Object.create(Request.prototype); | |
MyRequest.prototype.constructor = MyRequest; | |
function MyRequest(options, callbackfn) { | |
"use strict"; | |
if (callbackfn) | |
options.callback = callbackfn; | |
options.method = options.method || 'POST'; | |
Request.prototype.constructor.call(this, options); | |
} | |
MyRequest.prototype.form = function (form) { | |
"use strict"; | |
if (form) { | |
this.setHeader('content-type', 'application/x-www-form-urlencoded; charset=utf-8'); | |
this.body = querystring.stringify(form).toString('utf8'); | |
return this; | |
} | |
else | |
return Request.prototype.form.apply(this, arguments); | |
}; | |
module.exports = MyRequest; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The standard npm Request module formats multiple selectable form data as follows:
foo[0]=value0&foo[1]=value1
If, instead, you want
foo=value0&foo=value1
this little subclass will do the trick. Note: you cannot use the lowercase factory-like methodsrequest.get()
,request.post()
with this class. You must simply govar ignored = new MyRequest(options);
For now, I'm not doing any tricky multiple varargs stuff, just pass in an options object.