Form post method
This commit is contained in:
@@ -3,7 +3,8 @@
|
|||||||
var http = require("http");
|
var http = require("http");
|
||||||
var https = require("https");
|
var https = require("https");
|
||||||
var url = require("url");
|
var url = require("url");
|
||||||
|
var querystring = require("querystring");
|
||||||
|
var stream = require("stream");
|
||||||
|
|
||||||
function Cookie(rawCookie) {
|
function Cookie(rawCookie) {
|
||||||
var cookieRegex = /([^,; =]+)(?:=([^;]*)(?:[ ;]+|$))/g;
|
var cookieRegex = /([^,; =]+)(?:=([^;]*)(?:[ ;]+|$))/g;
|
||||||
@@ -118,7 +119,7 @@ module.exports.HttpClient = function Browser (_baseUrl, _headers) {
|
|||||||
this.headers["Accept-Language"] = "en-US";
|
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";
|
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) {
|
this.put = function(_path, _data) {
|
||||||
return query({
|
return query({
|
||||||
method : "PUT",
|
method : "PUT",
|
||||||
@@ -171,6 +180,11 @@ module.exports.HttpClient = function Browser (_baseUrl, _headers) {
|
|||||||
|
|
||||||
pathUrl.headers = instance.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);
|
var cookies = cookieJar.get(pathUrl.pathname);
|
||||||
|
|
||||||
if ( pathUrl.pathname !== "/") {
|
if ( pathUrl.pathname !== "/") {
|
||||||
@@ -233,10 +247,8 @@ module.exports.HttpClient = function Browser (_baseUrl, _headers) {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
decoder = response;
|
decoder = response;
|
||||||
response.setEncoding("utf8");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fulfill(decoder);
|
fulfill(decoder);
|
||||||
}
|
}
|
||||||
else if (response.statusCode < 400) {
|
else if (response.statusCode < 400) {
|
||||||
@@ -254,7 +266,15 @@ module.exports.HttpClient = function Browser (_baseUrl, _headers) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (_request.data) {
|
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();
|
request.end();
|
||||||
|
|||||||
Reference in New Issue
Block a user