Merge pull request #892 from ParsePlatform/flovilmart.httpRequestDefaultContentType
Default body to querystring, null encoding
This commit is contained in:
21
src/cloud-code/HTTPResponse.js
Normal file
21
src/cloud-code/HTTPResponse.js
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
export default class HTTPResponse {
|
||||
constructor(response) {
|
||||
this.status = response.statusCode;
|
||||
this.headers = response.headers;
|
||||
this.buffer = response.body;
|
||||
this.cookies = response.headers["set-cookie"];
|
||||
}
|
||||
|
||||
get text() {
|
||||
return this.buffer.toString('utf-8');
|
||||
}
|
||||
get data() {
|
||||
if (!this._data) {
|
||||
try {
|
||||
this._data = JSON.parse(this.text);
|
||||
} catch (e) {}
|
||||
}
|
||||
return this._data;
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,36 @@
|
||||
var request = require("request"),
|
||||
querystring = require('querystring'),
|
||||
Parse = require('parse/node').Parse;
|
||||
import request from 'request';
|
||||
import Parse from 'parse/node';
|
||||
import HTTPResponse from './HTTPResponse';
|
||||
import querystring from 'querystring';
|
||||
|
||||
var encodeBody = function(body, headers = {}) {
|
||||
var encodeBody = function({body, headers = {}}) {
|
||||
if (typeof body !== 'object') {
|
||||
return body;
|
||||
return {body, headers};
|
||||
}
|
||||
var contentTypeKeys = Object.keys(headers).filter((key) => {
|
||||
return key.match(/content-type/i) != null;
|
||||
});
|
||||
|
||||
if (contentTypeKeys.length == 1) {
|
||||
if (contentTypeKeys.length == 0) {
|
||||
// no content type
|
||||
// As per https://parse.com/docs/cloudcode/guide#cloud-code-advanced-sending-a-post-request the default encoding is supposedly x-www-form-urlencoded
|
||||
|
||||
body = querystring.stringify(body);
|
||||
headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
} else {
|
||||
/* istanbul ignore next */
|
||||
if (contentTypeKeys.length > 1) {
|
||||
console.error('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)) {
|
||||
body = Object.keys(body).map(function(key){
|
||||
return `${key}=${encodeURIComponent(body[key])}`
|
||||
}).join("&");
|
||||
body = querystring.stringify(body);
|
||||
}
|
||||
}
|
||||
return body;
|
||||
return {body, headers};
|
||||
}
|
||||
|
||||
module.exports = function(options) {
|
||||
@@ -32,7 +42,7 @@ module.exports = function(options) {
|
||||
delete options.success;
|
||||
delete options.error;
|
||||
delete options.uri; // not supported
|
||||
options.body = encodeBody(options.body, options.headers);
|
||||
options = Object.assign(options, encodeBody(options));
|
||||
// set follow redirects to false by default
|
||||
options.followRedirect = options.followRedirects == true;
|
||||
// support params options
|
||||
@@ -41,6 +51,8 @@ module.exports = function(options) {
|
||||
} else if (typeof options.params === 'string') {
|
||||
options.qs = querystring.parse(options.params);
|
||||
}
|
||||
// force the response as a buffer
|
||||
options.encoding = null;
|
||||
|
||||
request(options, (error, response, body) => {
|
||||
if (error) {
|
||||
@@ -49,15 +61,8 @@ module.exports = function(options) {
|
||||
}
|
||||
return promise.reject(error);
|
||||
}
|
||||
var httpResponse = {};
|
||||
httpResponse.status = response.statusCode;
|
||||
httpResponse.headers = response.headers;
|
||||
httpResponse.buffer = new Buffer(response.body);
|
||||
httpResponse.cookies = response.headers["set-cookie"];
|
||||
httpResponse.text = response.body;
|
||||
try {
|
||||
httpResponse.data = JSON.parse(response.body);
|
||||
} catch (e) {}
|
||||
let httpResponse = new HTTPResponse(response);
|
||||
|
||||
// Consider <200 && >= 400 as errors
|
||||
if (httpResponse.status < 200 || httpResponse.status >= 400) {
|
||||
if (callbacks.error) {
|
||||
|
||||
Reference in New Issue
Block a user