From 87589646104f3cefcd49b50c6410a612d6cbe60d Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 6 Jul 2018 19:38:47 -0600 Subject: [PATCH] FormData support with form-data --- examples/form-data.js | 30 +++++++++++++++++++++++++ index.js | 52 ++++++++++++++++++++++++++++++++++++++----- package.json | 3 +++ tests/tcp-listener.js | 11 +++++++++ 4 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 examples/form-data.js create mode 100644 tests/tcp-listener.js diff --git a/examples/form-data.js b/examples/form-data.js new file mode 100644 index 0000000..8edfe7f --- /dev/null +++ b/examples/form-data.js @@ -0,0 +1,30 @@ +'use strict'; + +//var request = require('@coolaj86/urequest'); +// To check and make sure the outputs are the same +//var request = require('request'); +var request = require('../'); + +// will redirect to https://www.github.com and then https://github.com +//request('http://www.github.com', function (error, response, body) { +request( + //{ url: 'http://postb.in/syfxxnko' + { url: 'http://localhost:3007/form-data/' + , headers: { + 'X-Foo': 'Bar' + } + , formData: { + foo: 'bar' + , baz: require('fs').createReadStream(require('path').join(__dirname, 'get-to-json.js')) + } + } +, function (error, response, body) { + if (error) { + console.log('error:', error); // Print the error if one occurred + return; + } + console.log('statusCode:', response.statusCode); // The final statusCode + console.log('Final href:', response.request.uri.href); // The final URI + console.log('Body Length:', body.length); // body length + } +); diff --git a/index.js b/index.js index 22585d3..96f6cf0 100644 --- a/index.js +++ b/index.js @@ -150,6 +150,10 @@ function setDefaults(defs) { var req; var finalOpts = {}; var _body; + var MyFormData; + var form; + var formHeaders; + var requester; if (opts.body) { if (true === opts.json) { @@ -174,26 +178,62 @@ function setDefaults(defs) { // chunked is generally only well-supported downstream //finalOpts.headers['Content-Length'] = _body.byteLength || _body.length; } + if (opts.formData) { + try { + MyFormData = opts.FormData || require('form-data'); + // potential options https://github.com/felixge/node-combined-stream/blob/master/lib/combined_stream.js#L7-L15 + } catch(e) { + console.error("urequest does not include extra dependencies by default"); + console.error("if you need to use 'form-data' you may install it, like so:"); + console.error(" npm install --save form-data"); + cb(e); + return; + } + try { + form = new MyFormData(); + Object.keys(opts.formData).forEach(function (key) { + form.append(key, opts.formData[key]); + }); + } catch(e) { + cb(e); + return; + } + formHeaders = form.getHeaders(); + Object.keys(formHeaders).forEach(function (header) { + finalOpts.headers[header] = formHeaders[header]; + }); + } // TODO support unix sockets if ('https:' === finalOpts.protocol) { // https://nodejs.org/api/https.html#https_https_request_options_callback debug("\n[urequest] https.request(opts):"); debug(finalOpts); - req = https.request(finalOpts, onResponse); + requester = https; } else if ('http:' === finalOpts.protocol) { // https://nodejs.org/api/http.html#http_http_request_options_callback debug("\n[urequest] http.request(opts):"); debug(finalOpts); - req = http.request(finalOpts, onResponse); + requester = http; } else { cb(new Error("unknown protocol: '" + opts.uri.protocol + "'")); return; } - req.on('error', function (e) { - cb(e); - }); + if (form) { + debug("\n[urequest] '" + finalOpts.method + "' (request) form"); + debug(formHeaders); + // generally uploads don't use Chunked Encoding (some systems have issues with it) + // and I don't want to do the work to calculate the content-lengths. This seems to work. + form.submit(finalOpts, onResponse); + //req = requester.request(finalOpts, onResponse); + //req.on('error', cb); + //form.pipe(req); + return; + } + + req = requester.request(finalOpts, onResponse); + req.on('error', cb); if (_body) { debug("\n[urequest] '" + finalOpts.method + "' (request) body"); @@ -320,6 +360,8 @@ module.exports._keys = Object.keys(_defaults).concat([ 'encoding' , 'body' , 'json' +, 'formData' +, 'FormData' ]); module.exports.debug = (-1 !== (process.env.NODE_DEBUG||'').split(/\s+/g).indexOf('urequest')); diff --git a/package.json b/package.json index 2098082..4db3f16 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,9 @@ "version": "1.1.4", "description": "A lightweight drop-in replacement for request", "main": "index.js", + "files": [ + "lib" + ], "directories": { "example": "examples" }, diff --git a/tests/tcp-listener.js b/tests/tcp-listener.js new file mode 100644 index 0000000..bca5456 --- /dev/null +++ b/tests/tcp-listener.js @@ -0,0 +1,11 @@ +'use strict'; + +var net = require('net'); +var server = net.createServer(function (socket) { + socket.on('data', function (chunk) { + console.info(chunk.toString('utf8')); + }); +}) +server.listen(3007, function () { + console.info("Listening on", this.address()); +});