Skip to content

Commit 0c446b9

Browse files
committed
Merge pull request #912 from carmenlau/911-params-option
#911 support params option in Parse.Cloud.httpRequest
2 parents 241cd8c + 4400992 commit 0c446b9

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

spec/HTTPRequest.spec.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ app.get("/301", function(req, res){
2424

2525
app.post('/echo', function(req, res){
2626
res.json(req.body);
27-
})
27+
});
28+
29+
app.get('/qs', function(req, res){
30+
res.json(req.query);
31+
});
2832

2933
app.listen(13371);
3034

@@ -193,4 +197,35 @@ describe("httpRequest", () => {
193197
}
194198
});
195199
})
200+
201+
it("should params object to query string", (done) => {
202+
httpRequest({
203+
url: httpRequestServer+"/qs",
204+
params: {
205+
foo: "bar"
206+
}
207+
}).then(function(httpResponse){
208+
expect(httpResponse.status).toBe(200);
209+
expect(httpResponse.data).toEqual({foo: "bar"});
210+
done();
211+
}, function(){
212+
fail("should not fail");
213+
done();
214+
})
215+
});
216+
217+
it("should params string to query string", (done) => {
218+
httpRequest({
219+
url: httpRequestServer+"/qs",
220+
params: "foo=bar&foo2=bar2"
221+
}).then(function(httpResponse){
222+
expect(httpResponse.status).toBe(200);
223+
expect(httpResponse.data).toEqual({foo: "bar", foo2: 'bar2'});
224+
done();
225+
}, function(){
226+
fail("should not fail");
227+
done();
228+
})
229+
});
230+
196231
});

src/cloud-code/httpRequest.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var request = require("request"),
2+
querystring = require('querystring'),
23
Parse = require('parse/node').Parse;
34

45
var encodeBody = function(body, headers = {}) {
@@ -34,7 +35,13 @@ module.exports = function(options) {
3435
options.body = encodeBody(options.body, options.headers);
3536
// set follow redirects to false by default
3637
options.followRedirect = options.followRedirects == true;
37-
38+
// support params options
39+
if (typeof options.params === 'object') {
40+
options.qs = options.params;
41+
} else if (typeof options.params === 'string') {
42+
options.qs = querystring.parse(options.params);
43+
}
44+
3845
request(options, (error, response, body) => {
3946
if (error) {
4047
if (callbacks.error) {

0 commit comments

Comments
 (0)