Skip to content

Commit c85b962

Browse files
author
do-web
committed
Fix multiple request
1 parent 73699ab commit c85b962

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

index.js

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ module.exports = (function () {
4646
};
4747

4848
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);
5054
return this;
5155
};
5256

@@ -119,33 +123,43 @@ module.exports = (function () {
119123
return nHeaders;
120124
};
121125

126+
this._reset = () => {
127+
this.curl = new Curl();
128+
};
129+
122130
this._submit = () => {
123131
return new Promise((resolve, reject) => {
124132

125-
this.curl.on('end', function (statusCode, body, headers) {
133+
try {
134+
this.curl.on('end', function (statusCode, body, headers) {
126135

127-
headers = normalizeHeaders(headers);
136+
headers = normalizeHeaders(headers);
128137

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+
}
136146
}
137147
}
138-
}
139148

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+
}
149163
});
150164
};
151165

0 commit comments

Comments
 (0)