@@ -46,7 +46,11 @@ module.exports = (function () {
46
46
} ;
47
47
48
48
this . setBody = ( fieldsObj ) => {
49
- curl . setOpt ( Curl . option . POSTFIELDS , querystring . stringify ( fieldsObj ) ) ;
49
+
50
+ if ( typeof fieldsObj !== 'string' ) {
51
+ fieldsObj = querystring . stringify ( fieldsObj )
52
+ }
53
+ curl . setOpt ( Curl . option . POSTFIELDS , fieldsObj ) ;
50
54
return this ;
51
55
} ;
52
56
@@ -119,33 +123,43 @@ module.exports = (function () {
119
123
return nHeaders ;
120
124
} ;
121
125
126
+ this . _reset = ( ) => {
127
+ this . curl = new Curl ( ) ;
128
+ } ;
129
+
122
130
this . _submit = ( ) => {
123
131
return new Promise ( ( resolve , reject ) => {
124
132
125
- this . curl . on ( 'end' , function ( statusCode , body , headers ) {
133
+ try {
134
+ this . curl . on ( 'end' , function ( statusCode , body , headers ) {
126
135
127
- headers = normalizeHeaders ( headers ) ;
136
+ headers = normalizeHeaders ( headers ) ;
128
137
129
- if ( that . default . autoParse ) {
130
- if ( typeof headers [ 'content-type' ] !== 'undefined' &&
131
- headers [ 'content-type' ] . toLocaleLowerCase ( ) === 'application/json' ) {
132
- try {
133
- let jsonObj = JSON . parse ( body ) ;
134
- body = jsonObj ;
135
- } catch ( e ) {
138
+ if ( that . default . autoParse ) {
139
+ if ( typeof headers [ 'content-type' ] !== 'undefined' &&
140
+ headers [ 'content-type' ] . toLocaleLowerCase ( ) === 'application/json' ) {
141
+ try {
142
+ let jsonObj = JSON . parse ( body ) ;
143
+ body = jsonObj ;
144
+ } catch ( e ) {
145
+ }
136
146
}
137
147
}
138
- }
139
148
140
- resolve ( { statusCode, body, headers} ) ;
141
- this . close ( ) ;
142
- } ) ;
143
-
144
- this . curl . on ( 'error' , function ( ) {
145
- reject ( arguments ) ;
146
- this . close ( ) ;
147
- } ) ;
148
- this . curl . perform ( ) ;
149
+ resolve ( { statusCode, body, headers} ) ;
150
+ this . close ( ) ;
151
+ that . _reset ( ) ;
152
+ } ) ;
153
+
154
+ this . curl . on ( 'error' , function ( ) {
155
+ reject ( arguments ) ;
156
+ this . close ( ) ;
157
+ that . _reset ( ) ;
158
+ } ) ;
159
+ this . curl . perform ( ) ;
160
+ } catch ( e ) {
161
+ reject ( e ) ;
162
+ }
149
163
} ) ;
150
164
} ;
151
165
0 commit comments