Skip to content

#911 support params option in Parse.Cloud.httpRequest #912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion spec/HTTPRequest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ app.get("/301", function(req, res){

app.post('/echo', function(req, res){
res.json(req.body);
})
});

app.get('/qs', function(req, res){
res.json(req.query);
});

app.listen(13371);

Expand Down Expand Up @@ -193,4 +197,35 @@ describe("httpRequest", () => {
}
});
})

it("should params object to query string", (done) => {
httpRequest({
url: httpRequestServer+"/qs",
params: {
foo: "bar"
}
}).then(function(httpResponse){
expect(httpResponse.status).toBe(200);
expect(httpResponse.data).toEqual({foo: "bar"});
done();
}, function(){
fail("should not fail");
done();
})
});

it("should params string to query string", (done) => {
httpRequest({
url: httpRequestServer+"/qs",
params: "foo=bar&foo2=bar2"
}).then(function(httpResponse){
expect(httpResponse.status).toBe(200);
expect(httpResponse.data).toEqual({foo: "bar", foo2: 'bar2'});
done();
}, function(){
fail("should not fail");
done();
})
});

});
9 changes: 8 additions & 1 deletion src/cloud-code/httpRequest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var request = require("request"),
querystring = require('querystring'),
Parse = require('parse/node').Parse;

var encodeBody = function(body, headers = {}) {
Expand Down Expand Up @@ -34,7 +35,13 @@ module.exports = function(options) {
options.body = encodeBody(options.body, options.headers);
// set follow redirects to false by default
options.followRedirect = options.followRedirects == true;

// support params options
if (typeof options.params === 'object') {
options.qs = options.params;
} else if (typeof options.params === 'string') {
options.qs = querystring.parse(options.params);
}

request(options, (error, response, body) => {
if (error) {
if (callbacks.error) {
Expand Down