Skip to content
Browse files

Added support for content-type application/json for post and push req…

…uests. Simply specify "dataFormat":"JSON" in your API endpoint config.
  • Loading branch information...
1 parent 5e9f9d4 commit e50952d0a0db0458fdeea665fd19c28c1bb106ce @adamkaplan committed Sep 13, 2011
Showing with 28 additions and 3 deletions.
  1. +27 −3 app.js
  2. +1 −0 views/api.jade
View
30 app.js
@@ -274,6 +274,7 @@ function processRequest(req, res, next) {
params = reqQuery.params || {},
methodURL = reqQuery.methodUri,
httpMethod = reqQuery.httpMethod,
+ dataFormat = reqQuery.dataFormat,
apiKey = reqQuery.apiKey,
apiSecret = reqQuery.apiSecret,
apiName = reqQuery.apiName
@@ -310,9 +311,14 @@ function processRequest(req, res, next) {
host: baseHostUrl,
port: baseHostPort,
method: httpMethod,
- path: apiConfig.publicPath + methodURL + ((paramString.length > 0) ? '?' + paramString : "")
+ path: apiConfig.publicPath + methodURL
};
+ // Append URL parameter list to GET, if needed
+ if (httpMethod == 'GET') {
+ options.path += ((paramString.length > 0) ? '?' + paramString : "")
+ }
+
if (apiConfig.oauth) {
console.log('Using OAuth');
@@ -499,8 +505,20 @@ function processRequest(req, res, next) {
options.headers = headers;
}
- if (!options.headers['Content-Length']) {
- options.headers['Content-Length'] = 0;
+ var sendData = ''
+ if (options.method == 'GET' || options.method == 'DELETE') {
+ if (!options.headers['Content-Length']) {
+ options.headers['Content-Length'] = 0;
+ }
+ }
+ else if (options.method == 'POST' || options.method == 'PUT') {
+ if (dataFormat && dataFormat.match(/^json$/i)) {
+ options.headers['Content-Type'] = 'application/json';
+ sendData = JSON.stringify(params)
+ } else {
+ options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
+ sendData = paramString
+ }
}
if (config.debug) {
@@ -559,6 +577,12 @@ function processRequest(req, res, next) {
};
});
+ if (sendData.length) {
+ if (config.debug) {
+ console.log("Request Body: "+sendData)
+ }
+ apiCall.write(sendData)
+ }
apiCall.end();
}
}
View
1 views/api.jade
@@ -70,6 +70,7 @@ ul
span.name #{method.MethodName}
span.uri #{method.URI}
form.hidden
+ input(type='hidden', name='dataFormat', value=method.DataFormat)
input(type='hidden', name='httpMethod', value=method.HTTPMethod)
input(type='hidden', name='oauth', value=secure)
input(type='hidden', name='methodUri', value=method.URI)

0 comments on commit e50952d

Please sign in to comment.
Something went wrong with that request. Please try again.