1- var request = require ( "request" ) ,
2- querystring = require ( 'querystring' ) ,
3- Parse = require ( 'parse/node' ) . Parse ;
1+ import request from 'request' ;
2+ import Parse from 'parse/node' ;
3+ import HTTPResponse from './HTTPResponse' ;
4+ import querystring from 'querystring' ;
45
5- var encodeBody = function ( body , headers = { } ) {
6+ var encodeBody = function ( { body, headers = { } } ) {
67 if ( typeof body !== 'object' ) {
7- return body ;
8+ return { body, headers } ;
89 }
910 var contentTypeKeys = Object . keys ( headers ) . filter ( ( key ) => {
1011 return key . match ( / c o n t e n t - t y p e / i) != null ;
1112 } ) ;
1213
13- if ( contentTypeKeys . length == 1 ) {
14+ if ( contentTypeKeys . length == 0 ) {
15+ // no content type
16+ // As per https://parse.com/docs/cloudcode/guide#cloud-code-advanced-sending-a-post-request the default encoding is supposedly x-www-form-urlencoded
17+
18+ body = querystring . stringify ( body ) ;
19+ headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
20+ } else {
21+ /* istanbul ignore next */
22+ if ( contentTypeKeys . length > 1 ) {
23+ console . error ( 'multiple content-type headers are set.' ) ;
24+ }
25+ // There maybe many, we'll just take the 1st one
1426 var contentType = contentTypeKeys [ 0 ] ;
1527 if ( headers [ contentType ] . match ( / a p p l i c a t i o n \/ j s o n / i) ) {
1628 body = JSON . stringify ( body ) ;
1729 } else if ( headers [ contentType ] . match ( / a p p l i c a t i o n \/ x - w w w - f o r m - u r l e n c o d e d / i) ) {
18- body = Object . keys ( body ) . map ( function ( key ) {
19- return `${ key } =${ encodeURIComponent ( body [ key ] ) } `
20- } ) . join ( "&" ) ;
30+ body = querystring . stringify ( body ) ;
2131 }
2232 }
23- return body ;
33+ return { body, headers } ;
2434}
2535
2636module . exports = function ( options ) {
@@ -32,7 +42,7 @@ module.exports = function(options) {
3242 delete options . success ;
3343 delete options . error ;
3444 delete options . uri ; // not supported
35- options . body = encodeBody ( options . body , options . headers ) ;
45+ options = Object . assign ( options , encodeBody ( options ) ) ;
3646 // set follow redirects to false by default
3747 options . followRedirect = options . followRedirects == true ;
3848 // support params options
@@ -41,6 +51,8 @@ module.exports = function(options) {
4151 } else if ( typeof options . params === 'string' ) {
4252 options . qs = querystring . parse ( options . params ) ;
4353 }
54+ // force the response as a buffer
55+ options . encoding = null ;
4456
4557 request ( options , ( error , response , body ) => {
4658 if ( error ) {
@@ -49,15 +61,8 @@ module.exports = function(options) {
4961 }
5062 return promise . reject ( error ) ;
5163 }
52- var httpResponse = { } ;
53- httpResponse . status = response . statusCode ;
54- httpResponse . headers = response . headers ;
55- httpResponse . buffer = new Buffer ( response . body ) ;
56- httpResponse . cookies = response . headers [ "set-cookie" ] ;
57- httpResponse . text = response . body ;
58- try {
59- httpResponse . data = JSON . parse ( response . body ) ;
60- } catch ( e ) { }
64+ let httpResponse = new HTTPResponse ( response ) ;
65+
6166 // Consider <200 && >= 400 as errors
6267 if ( httpResponse . status < 200 || httpResponse . status >= 400 ) {
6368 if ( callbacks . error ) {
0 commit comments