Merge pull request #892 from ParsePlatform/flovilmart.httpRequestDefaultContentType
Default body to querystring, null encoding
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
var httpRequest = require("../src/cloud-code/httpRequest"),
|
var httpRequest = require("../src/cloud-code/httpRequest"),
|
||||||
bodyParser = require('body-parser'),
|
bodyParser = require('body-parser'),
|
||||||
express = require("express");
|
express = require("express");
|
||||||
@@ -158,28 +160,44 @@ describe("httpRequest", () => {
|
|||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
it("should encode a JSON body", (done) => {
|
|
||||||
|
|
||||||
var result = httpRequest.encodeBody({"foo": "bar"}, {'Content-Type': 'application/json'});
|
it("should encode a query string body by default", (done) => {
|
||||||
expect(result).toEqual('{"foo":"bar"}');
|
let options = {
|
||||||
|
body: {"foo": "bar"},
|
||||||
|
}
|
||||||
|
let result = httpRequest.encodeBody(options);
|
||||||
|
expect(result.body).toEqual('foo=bar');
|
||||||
|
expect(result.headers['Content-Type']).toEqual('application/x-www-form-urlencoded');
|
||||||
|
done();
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should encode a JSON body", (done) => {
|
||||||
|
let options = {
|
||||||
|
body: {"foo": "bar"},
|
||||||
|
headers: {'Content-Type': 'application/json'}
|
||||||
|
}
|
||||||
|
let result = httpRequest.encodeBody(options);
|
||||||
|
expect(result.body).toEqual('{"foo":"bar"}');
|
||||||
done();
|
done();
|
||||||
|
|
||||||
})
|
})
|
||||||
it("should encode a www-form body", (done) => {
|
it("should encode a www-form body", (done) => {
|
||||||
|
let options = {
|
||||||
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'cOntent-tYpe': 'application/x-www-form-urlencoded'});
|
body: {"foo": "bar", "bar": "baz"},
|
||||||
expect(result).toEqual("foo=bar&bar=baz");
|
headers: {'cOntent-tYpe': 'application/x-www-form-urlencoded'}
|
||||||
|
}
|
||||||
|
let result = httpRequest.encodeBody(options);
|
||||||
|
expect(result.body).toEqual("foo=bar&bar=baz");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
it("should not encode a wrong content type", (done) => {
|
it("should not encode a wrong content type", (done) => {
|
||||||
|
let options = {
|
||||||
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'cOntent-tYpe': 'mime/jpeg'});
|
body:{"foo": "bar", "bar": "baz"},
|
||||||
expect(result).toEqual({"foo": "bar", "bar": "baz"});
|
headers: {'cOntent-tYpe': 'mime/jpeg'}
|
||||||
done();
|
}
|
||||||
});
|
let result = httpRequest.encodeBody(options);
|
||||||
it("should not encode when missing content type", (done) => {
|
expect(result.body).toEqual({"foo": "bar", "bar": "baz"});
|
||||||
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'X-Custom-Header': 'my-header'});
|
|
||||||
expect(result).toEqual({"foo": "bar", "bar": "baz"});
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -196,6 +214,17 @@ describe("httpRequest", () => {
|
|||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should get a cat image', (done) => {
|
||||||
|
httpRequest({
|
||||||
|
url: 'http://thecatapi.com/api/images/get?format=src&type=jpg',
|
||||||
|
followRedirects: true
|
||||||
|
}).then((res) => {
|
||||||
|
expect(res.buffer).not.toBe(null);
|
||||||
|
expect(res.text).not.toBe(null);
|
||||||
|
done();
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should params object to query string", (done) => {
|
it("should params object to query string", (done) => {
|
||||||
|
|||||||
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"),
|
import request from 'request';
|
||||||
querystring = require('querystring'),
|
import Parse from 'parse/node';
|
||||||
Parse = require('parse/node').Parse;
|
import HTTPResponse from './HTTPResponse';
|
||||||
|
import querystring from 'querystring';
|
||||||
|
|
||||||
var encodeBody = function(body, headers = {}) {
|
var encodeBody = function({body, headers = {}}) {
|
||||||
if (typeof body !== 'object') {
|
if (typeof body !== 'object') {
|
||||||
return body;
|
return {body, headers};
|
||||||
}
|
}
|
||||||
var contentTypeKeys = Object.keys(headers).filter((key) => {
|
var contentTypeKeys = Object.keys(headers).filter((key) => {
|
||||||
return key.match(/content-type/i) != null;
|
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];
|
var contentType = contentTypeKeys[0];
|
||||||
if (headers[contentType].match(/application\/json/i)) {
|
if (headers[contentType].match(/application\/json/i)) {
|
||||||
body = JSON.stringify(body);
|
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 = Object.keys(body).map(function(key){
|
body = querystring.stringify(body);
|
||||||
return `${key}=${encodeURIComponent(body[key])}`
|
|
||||||
}).join("&");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return body;
|
return {body, headers};
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function(options) {
|
module.exports = function(options) {
|
||||||
@@ -32,7 +42,7 @@ 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
|
||||||
options.body = encodeBody(options.body, options.headers);
|
options = Object.assign(options, encodeBody(options));
|
||||||
// set follow redirects to false by default
|
// set follow redirects to false by default
|
||||||
options.followRedirect = options.followRedirects == true;
|
options.followRedirect = options.followRedirects == true;
|
||||||
// support params options
|
// support params options
|
||||||
@@ -41,6 +51,8 @@ module.exports = function(options) {
|
|||||||
} else if (typeof options.params === 'string') {
|
} else if (typeof options.params === 'string') {
|
||||||
options.qs = querystring.parse(options.params);
|
options.qs = querystring.parse(options.params);
|
||||||
}
|
}
|
||||||
|
// force the response as a buffer
|
||||||
|
options.encoding = null;
|
||||||
|
|
||||||
request(options, (error, response, body) => {
|
request(options, (error, response, body) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
@@ -49,15 +61,8 @@ module.exports = function(options) {
|
|||||||
}
|
}
|
||||||
return promise.reject(error);
|
return promise.reject(error);
|
||||||
}
|
}
|
||||||
var httpResponse = {};
|
let httpResponse = new HTTPResponse(response);
|
||||||
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) {}
|
|
||||||
// Consider <200 && >= 400 as errors
|
// Consider <200 && >= 400 as errors
|
||||||
if (httpResponse.status < 200 || httpResponse.status >= 400) {
|
if (httpResponse.status < 200 || httpResponse.status >= 400) {
|
||||||
if (callbacks.error) {
|
if (callbacks.error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user