Adds support for application/x-www-form-urlencoded
- Now the body encoding is inferred on the headers as supposed
This commit is contained in:
@@ -139,6 +139,9 @@ describe("httpRequest", () => {
|
|||||||
body: {
|
body: {
|
||||||
foo: "bar"
|
foo: "bar"
|
||||||
},
|
},
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
success: function() { calls++; },
|
success: function() { calls++; },
|
||||||
error: function() { calls++; }
|
error: function() { calls++; }
|
||||||
}).then(function(httpResponse){
|
}).then(function(httpResponse){
|
||||||
@@ -150,5 +153,29 @@ describe("httpRequest", () => {
|
|||||||
fail("should not fail");
|
fail("should not fail");
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
|
});
|
||||||
|
it("should encode a JSON body", (done) => {
|
||||||
|
|
||||||
|
var result = httpRequest.encodeBody({"foo": "bar"}, {'Content-Type': 'application/json'});
|
||||||
|
expect(result).toEqual('{"foo":"bar"}');
|
||||||
|
done();
|
||||||
|
|
||||||
|
})
|
||||||
|
it("should encode a www-form body", (done) => {
|
||||||
|
|
||||||
|
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'cOntent-tYpe': 'application/x-www-form-urlencoded'});
|
||||||
|
expect(result).toEqual("foo=bar&bar=baz");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
it("should not encode a wrong content type", (done) => {
|
||||||
|
|
||||||
|
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'cOntent-tYpe': 'mime/jpeg'});
|
||||||
|
expect(result).toEqual({"foo": "bar", "bar": "baz"});
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
it("should not encode when missing content type", (done) => {
|
||||||
|
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'X-Custom-Header': 'my-header'});
|
||||||
|
expect(result).toEqual({"foo": "bar", "bar": "baz"});
|
||||||
|
done();
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,27 @@
|
|||||||
var request = require("request"),
|
var request = require("request"),
|
||||||
Parse = require('parse/node').Parse;
|
Parse = require('parse/node').Parse;
|
||||||
|
|
||||||
|
var encodeBody = function(body, headers = {}) {
|
||||||
|
if (typeof body !== 'object') {
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
var contentTypeKeys = Object.keys(headers).filter((key) => {
|
||||||
|
return key.match(/content-type/i) != null;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (contentTypeKeys.length == 1) {
|
||||||
|
var contentType = contentTypeKeys[0];
|
||||||
|
if (headers[contentType].match(/application\/json/i)) {
|
||||||
|
body = JSON.stringify(body);
|
||||||
|
} else if(headers[contentType].match(/application\/x-www-form-urlencoded/i)) {
|
||||||
|
body = Object.keys(body).map(function(key){
|
||||||
|
return `${key}=${encodeURIComponent(body[key])}`
|
||||||
|
}).join("&");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = function(options) {
|
module.exports = function(options) {
|
||||||
var promise = new Parse.Promise();
|
var promise = new Parse.Promise();
|
||||||
var callbacks = {
|
var callbacks = {
|
||||||
@@ -10,13 +31,9 @@ module.exports = function(options) {
|
|||||||
delete options.success;
|
delete options.success;
|
||||||
delete options.error;
|
delete options.error;
|
||||||
delete options.uri; // not supported
|
delete options.uri; // not supported
|
||||||
if (typeof options.body === 'object') {
|
options.body = encodeBody(options.body, options.headers);
|
||||||
options.body = JSON.stringify(options.body);
|
|
||||||
options.headers = options.headers || {};
|
|
||||||
options.headers['Content-Type'] = "application/json";
|
|
||||||
}
|
|
||||||
// set follow redirects to false by default
|
// set follow redirects to false by default
|
||||||
options.followRedirect = options.followRedirects == true ? true : false;
|
options.followRedirect = options.followRedirects == true;
|
||||||
|
|
||||||
request(options, (error, response, body) => {
|
request(options, (error, response, body) => {
|
||||||
var httpResponse = {};
|
var httpResponse = {};
|
||||||
@@ -42,4 +59,6 @@ module.exports = function(options) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
return promise;
|
return promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.encodeBody = encodeBody;
|
||||||
|
|||||||
Reference in New Issue
Block a user