Permalink
Browse files

Change `requestObj` addition to the `request()` method arguments to `…

…requestParams`. For GET and DELETE requests, the parameters are added to the query string. For POST and PUTs, where there is no request body or the request body is urlencoded, the parameters are added to the request body.
  • Loading branch information...
1 parent 5b3bb34 commit f0b8f2037d6d65f9711d391be583122551ed4085 @novemberborn novemberborn committed Jan 7, 2011
Showing with 28 additions and 9 deletions.
  1. +28 −9 lib/oauth.js
View
@@ -149,20 +149,26 @@ Client.prototype.request = function(originalRequest){
// Parse all request parameters into a flattened array of parameter pairs.
// Note that this array contains munged parameter names.
- var requestParams = [];
+ var requestParams = [], uncombinedRequestParams = [];
// Start with parameters that were defined in the query string
if(request.queryString){
querystring.parseToArray(requestParams, request.queryString);
}
// Allow parameters to be defined in object notation, this is *not* part of `http-client.request`!
// It saves an extra stringify+parse though.
- if(request.queryObj){
- for(var i in request.queryObj){
- if(request.queryObj.hasOwnProperty(i)){
- querystring.addToArray(requestParams, i, request.queryObj[i]);
+ if(request.requestParams){
+ for(var i in request.requestParams){
+ if(request.requestParams.hasOwnProperty(i)){
+ querystring.addToArray(uncombinedRequestParams, i, request.requestParams[i]);
}
}
}
+ // Send the parameters from `request.requestParams` in the query string
+ // for GET and DELETE requests. We immediately concat to the `requestParams` array,
+ // which is then built into the query string.
+ if(request.method == "GET" || request.method == "DELETE"){
+ requestParams = requestParams.concat(uncombinedRequestParams);
+ }
// Rebuild the query string
request.queryString = requestParams.reduce(function(qs, v, i){
return qs + (i % 2 ? "=" + querystring.escape(v) : (qs.length ? "&" : "") + querystring.escape(v));
@@ -173,6 +179,19 @@ Client.prototype.request = function(originalRequest){
if(request.headers && request.headers["Content-Type"] == "application/x-www-form-urlencoded"){
waitForBody = whenPromise(request.body.join(""), function(body){
querystring.parseToArray(requestParams, body);
+ return body;
+ });
+ }
+
+ // If we're a POST or PUT and are not sending any content, or are sending urlencoded content,
+ // add the `request.request` to the request body. If we are sending non-urlencoded content through
+ // a POST or PUT, the `request.requestParams` are ignored.
+ if(request.requestParams && (request.method == "POST" || request.method == "PUT") && (!request.headers || !request.headers["Content-Type"] || request.headers["Content-Type"] == "application/x-www-form-urlencoded")){
+ waitForBody = whenPromise(waitForBody, function(body){
+ requestParams = requestParams.concat(uncombinedRequestParams);
+ body = (body ? body + "&" : "") + querystring.stringify(request.requestParams);
+ request.body = [body];
+ request.headers["Content-Type"] = "application/x-www-form-urlencoded";
});
}
@@ -272,7 +291,7 @@ Client.prototype._signRequest = function(request, requestParams){
return request;
};
-Client.prototype.obtainTempCredentials = function(oauthParams, queryObj){
+Client.prototype.obtainTempCredentials = function(oauthParams, extraParams){
oauthParams = oauthParams || {};
if(this.callback && !oauthParams.oauth_callback){
oauthParams.oauth_callback = this.callback;
@@ -282,15 +301,15 @@ Client.prototype.obtainTempCredentials = function(oauthParams, queryObj){
method: "POST",
url: this.tempRequestUrl,
oauthParams: oauthParams,
- queryObj: queryObj || {}
+ requestParams: extraParams || {}
}).then(parseResponse);
};
-Client.prototype.obtainTokenCredentials = function(tokenIdentifier, tokenSecret, verifierToken, queryObj){
+Client.prototype.obtainTokenCredentials = function(tokenIdentifier, tokenSecret, verifierToken, extraParams){
return this.bind(tokenIdentifier, tokenSecret).request({
method: "POST",
url: this.tokenRequestUrl,
oauthParams: { oauth_verifier: verifierToken },
- queryObj: queryObj
+ requestParams: extraParams
}).then(parseResponse);
};

0 comments on commit f0b8f20

Please sign in to comment.