Skip to content

Instantly share code, notes, and snippets.

@MorganConrad
Created February 5, 2014 16:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MorganConrad/8827916 to your computer and use it in GitHub Desktop.
Save MorganConrad/8827916 to your computer and use it in GitHub Desktop.
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;
@MorganConrad
Copy link
Author

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 methods request.get(), request.post()with this class. You must simply go

var ignored = new MyRequest(options);

For now, I'm not doing any tricky multiple varargs stuff, just pass in an options object.

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