1
1
var request = require ( "request" ) ,
2
2
Parse = require ( 'parse/node' ) . Parse ;
3
3
4
+ var encodeBody = function ( body , headers = { } ) {
5
+ if ( typeof body !== 'object' ) {
6
+ return body ;
7
+ }
8
+ var contentTypeKeys = Object . keys ( headers ) . filter ( ( key ) => {
9
+ return key . match ( / c o n t e n t - t y p e / i) != null ;
10
+ } ) ;
11
+
12
+ if ( contentTypeKeys . length == 1 ) {
13
+ var contentType = contentTypeKeys [ 0 ] ;
14
+ if ( headers [ contentType ] . match ( / a p p l i c a t i o n \/ j s o n / i) ) {
15
+ body = JSON . stringify ( body ) ;
16
+ } 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) ) {
17
+ body = Object . keys ( body ) . map ( function ( key ) {
18
+ return `${ key } =${ encodeURIComponent ( body [ key ] ) } `
19
+ } ) . join ( "&" ) ;
20
+ }
21
+ }
22
+ return body ;
23
+ }
24
+
4
25
module . exports = function ( options ) {
5
26
var promise = new Parse . Promise ( ) ;
6
27
var callbacks = {
@@ -10,13 +31,9 @@ module.exports = function(options) {
10
31
delete options . success ;
11
32
delete options . error ;
12
33
delete options . uri ; // not supported
13
- if ( typeof options . body === 'object' ) {
14
- options . body = JSON . stringify ( options . body ) ;
15
- options . headers = options . headers || { } ;
16
- options . headers [ 'Content-Type' ] = "application/json" ;
17
- }
34
+ options . body = encodeBody ( options . body , options . headers ) ;
18
35
// set follow redirects to false by default
19
- options . followRedirect = options . followRedirects == true ? true : false ;
36
+ options . followRedirect = options . followRedirects == true ;
20
37
21
38
request ( options , ( error , response , body ) => {
22
39
var httpResponse = { } ;
@@ -42,4 +59,6 @@ module.exports = function(options) {
42
59
}
43
60
} ) ;
44
61
return promise ;
45
- } ;
62
+ } ;
63
+
64
+ module . exports . encodeBody = encodeBody ;
0 commit comments