Skip to content

Add support for FormData with POST requests. #81

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 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 10 additions & 2 deletions lib/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ exports.XMLHttpRequest = function() {
throw "INVALID_STATE_ERR: send has already been called";
}

var ssl = false, local = false;
var ssl = false, local = false, formData = false;
var url = Url.parse(settings.url);
var host;
// Determine the server
Expand Down Expand Up @@ -340,9 +340,15 @@ exports.XMLHttpRequest = function() {
headers["Authorization"] = "Basic " + authBuf.toString("base64");
}

// Determine whether data is FormData
formData = data && data.pipe && data.getHeaders && data.getLengthSync;

// Set content length header
if (settings.method === "GET" || settings.method === "HEAD") {
data = null;
} else if (formData) {
headers["Content-Length"] = data.getLengthSync();
headers["Content-Type"] = data.getHeaders()["content-type"];
} else if (data) {
headers["Content-Length"] = Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data);

Expand Down Expand Up @@ -445,7 +451,9 @@ exports.XMLHttpRequest = function() {
request = doRequest(options, responseHandler).on('error', errorHandler);

// Node 0.4 and later won't accept empty data. Make sure it's needed.
if (data) {
if (formData) {
data.pipe(request);
} else if (data) {
request.write(data);
}

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@
"lib": "./lib"
, "example": "./example"
}
, "devDependencies": {
"formidable": "^1.0.15"
, "form-data": "^0.1.4"
}
, "main": "./lib/XMLHttpRequest.js"
}

53 changes: 53 additions & 0 deletions tests/test-post-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var sys = require("util")
, assert = require("assert")
, XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest
, http = require("http")
, FormData = require("form-data")
, formidable = require("formidable")
, xhr;

// Test server
var method = "POST";
var server = http.createServer(function (req, res) {
// Check request method and URL
assert.equal(method, req.method);
assert.equal("/" + method, req.url);

var form = formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
assert.equal(err, null);
assert.equal(fields.key, "value");
var body = "Hello World";

res.writeHead(200, {
"Content-Type": "text/plain",
"Content-Length": Buffer.byteLength(body)
});
res.write(body);
res.end();
server.close();
});
}).listen(8000);

// Test POST with FormData
var form = new FormData();
form.append("key", "value");

function start() {
// Reset each time
xhr = new XMLHttpRequest();

xhr.onreadystatechange = function() {
if (this.readyState == 4) {
assert.equal("Hello World", this.responseText);
sys.puts("done");
}
};

var url = "http://localhost:8000/" + method;
xhr.open(method, url);
xhr.send(form);
}

sys.puts("Testing POST with FormData");
start();