Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support for more advanced forms #24

Closed
wants to merge 4 commits into from

4 participants

@adamkaplan

Added support for POST and PUT. These are helpful for using REST APIs... (though I now prefer not to use POST and PUT in practice)

Also added support for read-only and hidden fields. The former are displayed but not modifiable in the iodocs UI, while the latter are just hidden. These fields are generally helpful for supporting fields that are mandatory, but don't change often or ever.

adamkaplan added some commits
@adamkaplan adamkaplan Added support for content-type application/json for post and push req…
…uests. Simply specify "dataFormat":"JSON" in your API endpoint config.
e50952d
@adamkaplan adamkaplan Added support for true JSON values in PUT and POST data. Previously J…
…SON data would be double-quoted, which could cause parsing anomolies. Any field maked as type JSON will default to this mode.
9b289c6
@adamkaplan adamkaplan Added support for 'Hidden'='Y' and 'Readonly'='Y' for making fields h…
…idden or readonly (or both)
19814e8
@adamkaplan adamkaplan Merge remote-tracking branch 'upstream/master' c13acd9
@mansilladev mansilladev was assigned
@haimich

If this would be merged into the master it would help me a lot. Thanks for you work!

@haimich

I manually merged your code into my application and I'm trying to figure out how to configure the endpoint file to use json in my POST body. Could you please give me an example for the exact syntax?

@mansilladev
Owner

+1. Adam, can you send over your config file? Also, if you have it running in public area, can you send over URL? Thanks.

@adamkaplan

Sorry for missing your comments... I just happened to check up on this.

Here is a snipped of config that works for "HTTPMethod":"POST", or "HTTPMethod":"PUT",

{
   "MethodName":"Create",
   "Synopsis":"Creates a new message",
   "HTTPMethod":"POST",
   "DataFormat":"JSON",
   "URI":"generators",
   "parameters":[
      {
         "Name":"name",
         "Required":"Y",
         "Default":"",
         "Type":"String",
         "Description":"Fully qualified name"
      },
      {
         "Name":"parameters",
         "Required":"N",
         "Default":"{ }",
         "Type":"JSON",                                                                                                                                                                                                                                                       
         "Description":"JSON associative array of parameters"
      }
   ]
}
@alexadkins
Owner

POST/PUT support in commit 8ff62fc

@alexadkins alexadkins closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 13, 2011
  1. @adamkaplan

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

    adamkaplan authored
    …uests. Simply specify "dataFormat":"JSON" in your API endpoint config.
  2. @adamkaplan

    Added support for true JSON values in PUT and POST data. Previously J…

    adamkaplan authored
    …SON data would be double-quoted, which could cause parsing anomolies. Any field maked as type JSON will default to this mode.
Commits on Oct 26, 2011
  1. @adamkaplan

    Added support for 'Hidden'='Y' and 'Readonly'='Y' for making fields h…

    adamkaplan authored
    …idden or readonly (or both)
Commits on May 1, 2012
  1. @adamkaplan
This page is out of date. Refresh to see the latest.
Showing with 53 additions and 8 deletions.
  1. +37 −3 app.js
  2. +16 −5 views/api.jade
View
40 app.js
@@ -276,8 +276,10 @@ function processRequest(req, res, next) {
var reqQuery = req.body,
params = reqQuery.params || {},
+ jsonParams = reqQuery.jsonParams || {},
methodURL = reqQuery.methodUri,
httpMethod = reqQuery.httpMethod,
+ dataFormat = reqQuery.dataFormat,
apiKey = reqQuery.apiKey,
apiSecret = reqQuery.apiSecret,
apiName = reqQuery.apiName
@@ -302,6 +304,15 @@ function processRequest(req, res, next) {
}
}
+ // Extract any parameters that are marked as JSON since they will otherwise be quoted
+ for (var jsonparam in jsonParams) {
+ if (jsonParams.hasOwnProperty(jsonparam) && params.hasOwnProperty(jsonparam)) {
+ if (jsonParams[jsonparam]) {
+ params[jsonparam] = JSON.parse(params[jsonparam])
+ }
+ }
+ }
+
var baseHostInfo = apiConfig.baseURL.split(':');
var baseHostUrl = baseHostInfo[0],
baseHostPort = (baseHostInfo.length > 1) ? baseHostInfo[1] : "";
@@ -314,9 +325,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');
@@ -513,8 +529,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) {
@@ -573,6 +601,12 @@ function processRequest(req, res, next) {
};
});
+ if (sendData.length) {
+ if (config.debug) {
+ console.log("Request Body: "+sendData)
+ }
+ apiCall.write(sendData)
+ }
apiCall.end();
}
}
View
21 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)
@@ -96,20 +97,27 @@ ul
- else
- var required =false
- var className =''
- tr(class=className)
+ - var style =null
+ - if (parameter.Hidden ==true)
+ - style ='display: none'
+ tr(class=className, style=style)
td.name=parameter.Name
td.parameter
+ - if (parameter.Readonly =='Y')
+ - var roValue ='readonly'
+ - else
+ - var roValue =null
- if (parameter.Type =='enumerated')
- select(name='params[' + parameter.Name + ']', placeholder=className)
+ select(name='params[' + parameter.Name + ']', placeholder=className, readonly=roValue)
- if (parameter.Default =='')
- option(value='')
+ option(value='',readonly=roValue)
- each choice in parameter.EnumeratedList
- if (parameter.Default ==choice)
option(value=choice, selected=true) #{choice}
- else
option(value=choice) #{choice}
- else if (parameter.Type =='boolean')
- select(name='params[' + parameter.Name + ']', placeholder=className)
+ select(name='params[' + parameter.Name + ']', placeholder=className, readonly=roValue)
- if (parameter.Default =='')
option(value='')
- each choice in [apiInfo.booleanTrueVal,apiInfo.booleanFalseVal]
@@ -117,8 +125,11 @@ ul
option(value=choice, selected=true) #{choice}
- else
option(value=choice) #{choice}
+ - else if (parameter.Type == 'JSON' || parameter.Type == 'json')
+ input(name='jsonParams[' + parameter.Name + ']', type='hidden', value=1)
+ input(name='params[' + parameter.Name + ']', value=parameter.Default, placeholder=className, readonly=roValue)
- else
- input(name='params[' + parameter.Name + ']', value=parameter.Default, placeholder=className)
+ input(name='params[' + parameter.Name + ']', value=parameter.Default, placeholder=className, readonly=roValue)
td.type=parameter.Type
td.description=parameter.Description || 'No description'
- if (method.headers && method.headers.length > 0)
Something went wrong with that request. Please try again.