Skip to content

allow http header options #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 100 additions & 103 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,103 +1,100 @@
/**
* http-post
*
* (c) copyright 2012 Sam Thompson <[email protected]>
* License: The MIT License - http://opensource.org/licenses/mit-license.php
*/

module.exports = function(options, data, files, fn) {
if (typeof(files) == 'function' || typeof(files) == 'undefined') {
fn = files;
files = [];
}

if (typeof(fn) != 'function') {
fn = function() {};
}

if (typeof(options) == 'string') {
var options = require('url').parse(options);
}

var fs = require('fs');
var endl = "\r\n";
var length = 0;
var contentType = '';

// If we have files, we have to chose multipart, otherweise we just stringify the query
if (files.length) {
var boundary = '-----np' + Math.random();
var toWrite = [];

for(var k in data) {
toWrite.push('--' + boundary + endl);
toWrite.push('Content-Disposition: form-data; name="' + k + '"' + endl);
toWrite.push(endl);
toWrite.push(data[k] + endl);
}

var name = '', stats;
for (var k in files) {
if (fs.existsSync(files[k].path)) {
// Determine the name
name = (typeof(files[k].name) == 'string') ? files[k].name : files[k].path.replace(/\\/g,'/').replace( /.*\//, '' );

// Determine the size and store it in our files area
stats = fs.statSync(files[k].path);
files[k].length = stats.size;

toWrite.push('--' + boundary + endl);
toWrite.push('Content-Disposition: form-data; name="' + files[k].param + '"; filename="' + name + '"' + endl);
//toWrite.push('Content-Type: image/png');
toWrite.push(endl);
toWrite.push(files[k]);
}
}

// The final multipart terminator
toWrite.push('--' + boundary + '--' + endl);

// Now that toWrite is filled... we need to determine the size
for(var k in toWrite) {
length += toWrite[k].length;
}

contentType = 'multipart/form-data; boundary=' + boundary;
}
else {
data = require('querystring').stringify(data);

length = data.length;
contentType = 'application/x-www-form-urlencoded';
}

options.method = 'POST';
options.headers = {
'Content-Type': contentType,
'Content-Length': length
};

var req = require('http').request(options, function(responce) {
fn(responce);
});

// Multipart and form-urlencded work slightly differnetly for sending
if (files.length) {
for(var k in toWrite) {
if (typeof(toWrite[k]) == 'string') {
req.write(toWrite[k]);
}
else {
// @todo make it work better for larger files
req.write(fs.readFileSync(toWrite[k].path, 'binary'));
req.write(endl);
}
}
}
else {
req.write(data);
}
req.end();

return req;
}
/**
* http-post
*
* (c) copyright 2012 Sam Thompson <[email protected]>
* License: The MIT License - http://opensource.org/licenses/mit-license.php
*/

module.exports = function(options, data, files, fn) {
if (typeof(files) == 'function' || typeof(files) == 'undefined') {
fn = files;
files = [];
}

if (typeof(fn) != 'function') {
fn = function() {};
}

if (typeof(options) == 'string') {
var options = require('url').parse(options);
}

var fs = require('fs');
var endl = "\r\n";
var length = 0;
var contentType = '';

// If we have files, we have to chose multipart, otherweise we just stringify the query
if (files.length) {
var boundary = '-----np' + Math.random();
var toWrite = [];

for(var k in data) {
toWrite.push('--' + boundary + endl);
toWrite.push('Content-Disposition: form-data; name="' + k + '"' + endl);
toWrite.push(endl);
toWrite.push(data[k] + endl);
}

var name = '', stats;
for (var k in files) {
if (fs.existsSync(files[k].path)) {
// Determine the name
name = (typeof(files[k].name) == 'string') ? files[k].name : files[k].path.replace(/\\/g,'/').replace( /.*\//, '' );

// Determine the size and store it in our files area
stats = fs.statSync(files[k].path);
files[k].length = stats.size;

toWrite.push('--' + boundary + endl);
toWrite.push('Content-Disposition: form-data; name="' + files[k].param + '"; filename="' + name + '"' + endl);
//toWrite.push('Content-Type: image/png');
toWrite.push(endl);
toWrite.push(files[k]);
}
}

// The final multipart terminator
toWrite.push('--' + boundary + '--' + endl);

// Now that toWrite is filled... we need to determine the size
for(var k in toWrite) {
length += toWrite[k].length;
}

contentType = 'multipart/form-data; boundary=' + boundary;
}
else {
data = require('querystring').stringify(data);
length = data.length;
contentType = 'application/x-www-form-urlencoded';
}
options.method = 'POST';
options.headers = options.headers || {};
options.headers['Content-Length'] = length;
options.headers['Content-Type'] = contentType;

var req = require('http').request(options, function(responce) {
fn(responce);
});

// Multipart and form-urlencded work slightly differnetly for sending
if (files.length) {
for(var k in toWrite) {
if (typeof(toWrite[k]) == 'string') {
req.write(toWrite[k]);
}
else {
// @todo make it work better for larger files
req.write(fs.readFileSync(toWrite[k].path, 'binary'));
req.write(endl);
}
}
}
else {
req.write(data);
}
req.end();

return req;
}