@@ -3,11 +3,11 @@ import HTTPResponse from './HTTPResponse';
|
||||
import querystring from 'querystring';
|
||||
import log from '../logger';
|
||||
|
||||
var encodeBody = function({body, headers = {}}) {
|
||||
var encodeBody = function({ body, headers = {} }) {
|
||||
if (typeof body !== 'object') {
|
||||
return {body, headers};
|
||||
return { body, headers };
|
||||
}
|
||||
var contentTypeKeys = Object.keys(headers).filter((key) => {
|
||||
var contentTypeKeys = Object.keys(headers).filter(key => {
|
||||
return key.match(/content-type/i) != null;
|
||||
});
|
||||
|
||||
@@ -20,18 +20,23 @@ var encodeBody = function({body, headers = {}}) {
|
||||
} else {
|
||||
/* istanbul ignore next */
|
||||
if (contentTypeKeys.length > 1) {
|
||||
log.error('Parse.Cloud.httpRequest', 'multiple content-type headers are set.');
|
||||
log.error(
|
||||
'Parse.Cloud.httpRequest',
|
||||
'multiple content-type headers are set.'
|
||||
);
|
||||
}
|
||||
// There maybe many, we'll just take the 1st one
|
||||
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)) {
|
||||
} else if (
|
||||
headers[contentType].match(/application\/x-www-form-urlencoded/i)
|
||||
) {
|
||||
body = querystring.stringify(body);
|
||||
}
|
||||
}
|
||||
return {body, headers};
|
||||
}
|
||||
return { body, headers };
|
||||
};
|
||||
|
||||
/**
|
||||
* Makes an HTTP Request.
|
||||
@@ -61,12 +66,12 @@ var encodeBody = function({body, headers = {}}) {
|
||||
module.exports = function(options) {
|
||||
var callbacks = {
|
||||
success: options.success,
|
||||
error: options.error
|
||||
error: options.error,
|
||||
};
|
||||
delete options.success;
|
||||
delete options.error;
|
||||
delete options.uri; // not supported
|
||||
options = Object.assign(options, encodeBody(options));
|
||||
options = Object.assign(options, encodeBody(options));
|
||||
// set follow redirects to false by default
|
||||
options.followRedirect = options.followRedirects == true;
|
||||
// support params options
|
||||
|
||||
Reference in New Issue
Block a user