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

Already on GitHub? Sign in to your account

Support for supplying POST, DELETE and PUT parameters in the HTTP request body instead of GET parameters #33

Merged
merged 6 commits into from Aug 15, 2012

Conversation

Projects
None yet
5 participants
Contributor

martintajur commented Jun 28, 2012

I've implemented support for POST, DELETE and PUT request parameters as part of HTTP request body instead of supplying them as GET parameters.

Previously, for example, when supplying parameters for a POST request, these parameters got attached to the request as GET parameters not as part of the request body as per HTTP specification. This patch addresses this issue.

EDIT: I also added a support for EnumeratedDescription which enables a better way to describe enumerated field values. It renders the possible enumerated field values with descriptions as a definition list below the field description.

Please review and discuss, hope it finds good use :)

Contributor

martintajur commented Jun 28, 2012

I also added a support for EnumeratedDescription which enables a better way to describe enumerated field values. It renders the possible enumerated field values with descriptions as a definition list below the field description.

@martintajur martintajur commented on the diff Jun 28, 2012

README.md
-35. "Type" key value is *boolean* that will render a drop-down (select box) on the form for *true* and *false*.
@martintajur

martintajur Jun 28, 2012

Contributor

The 35 seemed like a typo in the readme.

@martintajur martintajur commented on the diff Jun 28, 2012

@@ -314,9 +314,13 @@ function processRequest(req, res, next) {
host: baseHostUrl,
port: baseHostPort,
method: httpMethod,
- path: apiConfig.publicPath + methodURL + ((paramString.length > 0) ? '?' + paramString : "")
+ path: apiConfig.publicPath + methodURL// + ((paramString.length > 0) ? '?' + paramString : "")
@martintajur

martintajur Jun 28, 2012

Contributor

I left the paramString addition in a comment but it could easily be removed altogether.

@martintajur martintajur commented on the diff Jun 28, 2012

@@ -514,7 +522,16 @@ function processRequest(req, res, next) {
}
if (!options.headers['Content-Length']) {
- options.headers['Content-Length'] = 0;
+ if (requestBody) {
+ options.headers['Content-Length'] = requestBody.length;
+ }
+ else {
+ options.headers['Content-Length'] = 0;
+ }
+ }
+
+ if (requestBody) {
+ options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
@martintajur

martintajur Jun 28, 2012

Contributor

If someone is looking for using a body in JSON format, this would be the right place to make an additional check in order to set a different Content-Type header.

@martintajur martintajur commented on the diff Jun 28, 2012

// Dynamic Helpers
// Passes variables to the view
app.dynamicHelpers({
session: function(req, res) {
// If api wasn't passed in as a parameter, check the path to see if it's there
- if (!req.params.api) {
- pathName = req.url.replace('/','');
- // Is it a valid API - if there's a config file we can assume so
- fs.stat('public/data/' + pathName + '.json', function (error, stats) {
- if (stats) {
- req.params.api = pathName;
- }
- });
- }
- // If the cookie says we're authed for this particular API, set the session to authed as well
+ if (!req.params.api) {
@martintajur

martintajur Jun 28, 2012

Contributor

Haven't really changed anything here but I think my editor swapped tabs to spaces, or vice versa.

Contributor

jplock commented Aug 5, 2012

+1, I hope this gets merged in.

whump commented Aug 7, 2012

It would also be useful to set the content type of POST and PUT bodies for APIs that only handle XML content.

Contributor

mansilladev commented Aug 15, 2012

I'm going to merge this in. I think a fast follow to this might be JSON encoding (JSON.stringify) with application/json content-type, eh? Thanks for your work on this. Love it.

mansilladev added a commit that referenced this pull request Aug 15, 2012

Merge pull request #33 from martintajur/master
Support for supplying POST, DELETE and PUT parameters in the HTTP request body instead of GET parameters; enumerated descriptions feature.

@mansilladev mansilladev merged commit 7ac39c7 into mashery:master Aug 15, 2012

@rickhanlonii rickhanlonii commented on the diff Nov 15, 2012

};
+ if (['POST','DELETE','PUT'].indexOf(httpMethod) !== -1) {
+ var requestBody = query.stringify(params);
@rickhanlonii

rickhanlonii Nov 15, 2012

If someone is looking for using a body in JSON format, this needs to be changed to JSON.stringify(params);

query.stringify(params) will return something like

value=someval

JSON.stringift(params) will return

{"value":"someval"}

Otherwise, you'll throw a parse error on the server-side which is expecting a JSON format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment