Skip to content

Commit 8d76066

Browse files
author
Dhwaneet Bhatt
authored
Merge pull request #666 from dhwaneetbhatt/develop
Do not add HTTP method explicitly in curl when not required
2 parents b13e4a5 + 14ade92 commit 8d76066

File tree

4 files changed

+373
-4
lines changed

4 files changed

+373
-4
lines changed

codegens/curl/lib/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var sanitize = require('./util').sanitize,
33
getUrlStringfromUrlObject = require('./util').getUrlStringfromUrlObject,
44
addFormParam = require('./util').addFormParam,
55
form = require('./util').form,
6+
shouldAddHttpMethod = require('./util').shouldAddHttpMethod,
67
_ = require('./lodash'),
78
self;
89

@@ -44,12 +45,14 @@ self = module.exports = {
4445
else {
4546
indent = ' ';
4647
}
48+
4749
if (request.method === 'HEAD') {
48-
snippet += ` ${form('-I', format)} ${quoteType + url + quoteType}`;
50+
snippet += ` ${form('-I', format)}`;
4951
}
50-
else {
51-
snippet += ` ${form('-X', format)} ${request.method} ${quoteType + url + quoteType}`;
52+
if (shouldAddHttpMethod(request, options)) {
53+
snippet += ` ${form('-X', format)} ${request.method}`;
5254
}
55+
snippet += ` ${quoteType + url + quoteType}`;
5356

5457
if (request.body && !request.headers.has('Content-Type')) {
5558
if (request.body.mode === 'file') {
@@ -236,6 +239,13 @@ self = module.exports = {
236239
default: true,
237240
description: 'Automatically follow HTTP redirects'
238241
},
242+
{
243+
name: 'Follow original HTTP method',
244+
id: 'followOriginalHttpMethod',
245+
type: 'boolean',
246+
default: false,
247+
description: 'Redirect with the original HTTP method instead of the default behavior of redirecting with GET'
248+
},
239249
{
240250
name: 'Trim request body fields',
241251
id: 'trimRequestBody',

codegens/curl/lib/util.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const _ = require('./lodash');
2+
13
var self = module.exports = {
24
/**
35
* sanitizes input string by handling escape characters eg: converts '''' to '\'\'', (" to \" and \ to \\ )
@@ -199,5 +201,78 @@ var self = module.exports = {
199201
contentType: contentType
200202
});
201203
}
204+
},
205+
206+
/**
207+
* @param {Object} body
208+
* @returns {boolean}
209+
*
210+
* Determines if a request body is actually empty.
211+
* This is needed because body.isEmpty() returns false for formdata
212+
* and urlencoded when they contain only disabled params which will not
213+
* be a part of the curl request.
214+
*/
215+
isBodyEmpty (body) {
216+
if (!body) {
217+
return true;
218+
}
219+
220+
if (body.isEmpty()) {
221+
return true;
222+
}
223+
224+
if (body.mode === 'formdata' || body.mode === 'urlencoded') {
225+
let memberCount = 0;
226+
body[body.mode] && body[body.mode].members && body[body.mode].members.forEach((param) => {
227+
if (!param.disabled) {
228+
memberCount += 1;
229+
}
230+
});
231+
232+
return memberCount === 0;
233+
}
234+
235+
return false;
236+
},
237+
238+
/**
239+
* Decide whether we should add the HTTP method explicitly to the cURL command.
240+
*
241+
* @param {Object} request
242+
* @param {Object} options
243+
*
244+
* @returns {Boolean}
245+
*/
246+
shouldAddHttpMethod: function (request, options) {
247+
const followRedirect = _.get(request, 'protocolProfileBehavior.followRedirects', options.followRedirect),
248+
followOriginalHttpMethod =
249+
_.get(request, 'protocolProfileBehavior.followOriginalHttpMethod', options.followOriginalHttpMethod),
250+
disableBodyPruning = _.get(request, 'protocolProfileBehavior.disableBodyPruning', true),
251+
isBodyEmpty = self.isBodyEmpty(request.body);
252+
253+
if (followRedirect && followOriginalHttpMethod) {
254+
return true;
255+
}
256+
257+
switch (request.method) {
258+
case 'HEAD':
259+
return false;
260+
case 'GET':
261+
// disableBodyPruning will generally not be present in the request
262+
// the only time it will be present, its value will be _false_
263+
// i.e. the user wants to prune the request body despite it being present
264+
if (!isBodyEmpty && disableBodyPruning) {
265+
return true;
266+
}
267+
268+
return false;
269+
case 'POST':
270+
return isBodyEmpty;
271+
case 'DELETE':
272+
case 'PUT':
273+
case 'PATCH':
274+
default:
275+
return true;
276+
}
202277
}
203278
};

0 commit comments

Comments
 (0)