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' ;
4
5
5
- var encodeBody = function ( body , headers = { } ) {
6
+ var encodeBody = function ( { body, headers = { } } ) {
6
7
if ( typeof body !== 'object' ) {
7
- return body ;
8
+ return { body, headers } ;
8
9
}
9
10
var contentTypeKeys = Object . keys ( headers ) . filter ( ( key ) => {
10
11
return key . match ( / c o n t e n t - t y p e / i) != null ;
11
12
} ) ;
12
13
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
14
26
var contentType = contentTypeKeys [ 0 ] ;
15
27
if ( headers [ contentType ] . match ( / a p p l i c a t i o n \/ j s o n / i) ) {
16
28
body = JSON . stringify ( body ) ;
17
29
} 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 ) ;
21
31
}
22
32
}
23
- return body ;
33
+ return { body, headers } ;
24
34
}
25
35
26
36
module . exports = function ( options ) {
@@ -32,7 +42,7 @@ module.exports = function(options) {
32
42
delete options . success ;
33
43
delete options . error ;
34
44
delete options . uri ; // not supported
35
- options . body = encodeBody ( options . body , options . headers ) ;
45
+ options = Object . assign ( options , encodeBody ( options ) ) ;
36
46
// set follow redirects to false by default
37
47
options . followRedirect = options . followRedirects == true ;
38
48
// support params options
@@ -41,6 +51,8 @@ module.exports = function(options) {
41
51
} else if ( typeof options . params === 'string' ) {
42
52
options . qs = querystring . parse ( options . params ) ;
43
53
}
54
+ // force the response as a buffer
55
+ options . encoding = null ;
44
56
45
57
request ( options , ( error , response , body ) => {
46
58
if ( error ) {
@@ -49,15 +61,8 @@ module.exports = function(options) {
49
61
}
50
62
return promise . reject ( error ) ;
51
63
}
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
+
61
66
// Consider <200 && >= 400 as errors
62
67
if ( httpResponse . status < 200 || httpResponse . status >= 400 ) {
63
68
if ( callbacks . error ) {
0 commit comments