Form post method

This commit is contained in:
2015-08-16 23:14:18 +02:00
parent 4fb388d6a1
commit 7a0962c50b

View File

@@ -3,7 +3,8 @@
var http = require("http");
var https = require("https");
var url = require("url");
var querystring = require("querystring");
var stream = require("stream");
function Cookie(rawCookie) {
var cookieRegex = /([^,; =]+)(?:=([^;]*)(?:[ ;]+|$))/g;
@@ -118,7 +119,7 @@ module.exports.HttpClient = function Browser (_baseUrl, _headers) {
this.headers["Accept-Language"] = "en-US";
}
if (this.headers["Accept"] === undefined) {
if (this.headers["Accept"] === undefined) {
this.headers["Accept"] = "text/plain,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
}
@@ -142,6 +143,14 @@ module.exports.HttpClient = function Browser (_baseUrl, _headers) {
});
};
this.postForm = function(_path, _data) {
return query({
method : "POST",
path : _path,
data : querystring.stringify(_data)
});
};
this.put = function(_path, _data) {
return query({
method : "PUT",
@@ -171,6 +180,11 @@ module.exports.HttpClient = function Browser (_baseUrl, _headers) {
pathUrl.headers = instance.headers;
if (_request.querystring) {
let encodedQueryString = querystring.stringify(_request.querystring);
pathUrl.search = pathUrl.search ? pathUrl.search + "&" + encodedQueryString : "?" + encodedQueryString;
}
var cookies = cookieJar.get(pathUrl.pathname);
if ( pathUrl.pathname !== "/") {
@@ -233,10 +247,8 @@ module.exports.HttpClient = function Browser (_baseUrl, _headers) {
}
else {
decoder = response;
response.setEncoding("utf8");
}
fulfill(decoder);
}
else if (response.statusCode < 400) {
@@ -254,7 +266,15 @@ module.exports.HttpClient = function Browser (_baseUrl, _headers) {
});
if (_request.data) {
request.write(_request.data);
if (_request.data instanceof stream) {
_request.data.pipe(request);
}
else if (_request.data instanceof String || typeof(_request.data) === "string") {
request.write(_request.data);
}
else {
request.write(JSON.stringify(_request.data));
}
}
request.end();