Skip to content

Commit 146f206

Browse files
committed
add custom curl target, fix urls not having encoded params
1 parent cb28c98 commit 146f206

4 files changed

Lines changed: 1849 additions & 5 deletions

File tree

src/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,21 +167,22 @@ HTTPSnippet.prototype.prepare = function (request) {
167167
// reset uriObj values for a clean url
168168
request.uriObj.query = null
169169
request.uriObj.search = null
170-
request.uriObj.path = request.uriObj.pathname
170+
request.uriObj.path = decodeURIComponent(request.uriObj.pathname)
171171

172172
// keep the base url clean of queryString
173-
request.url = url.format(request.uriObj)
173+
request.url = decodeURIComponent(url.format(request.uriObj))
174174

175175
// update the uri object
176+
request.uriObj.href = decodeURIComponent(url.format(request.uriObj))
176177
request.uriObj.query = request.queryObj
177178
request.uriObj.search = qs.stringify(request.queryObj)
178179

179180
if (request.uriObj.search) {
180-
request.uriObj.path = request.uriObj.pathname + '?' + request.uriObj.search
181+
request.uriObj.path = decodeURIComponent(request.uriObj.pathname) + '?' + request.uriObj.search
181182
}
182183

183184
// construct a full url
184-
request.fullUrl = url.format(request.uriObj)
185+
request.fullUrl = decodeURIComponent(url.format(request.uriObj))
185186

186187
return request
187188
}

src/targets/shell/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ module.exports = {
1010

1111
curl: require('./curl'),
1212
httpie: require('./httpie'),
13-
wget: require('./wget')
13+
wget: require('./wget'),
14+
yapstonecurl: require('./yapstonecurl')
1415
}

src/targets/shell/yapstonecurl.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for the Shell using cURL.
4+
*
5+
* @author
6+
* @AhmadNassri
7+
*
8+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+
*/
10+
11+
'use strict'
12+
13+
var util = require('util')
14+
var helpers = require('../../helpers/shell')
15+
var CodeBuilder = require('../../helpers/code-builder')
16+
17+
module.exports = function (source, options) {
18+
var opts = util._extend({
19+
indent: ' ',
20+
short: false,
21+
binary: false
22+
}, options)
23+
24+
var code = new CodeBuilder(opts.indent, opts.indent !== false ? ' \\\n' + opts.indent : ' ')
25+
26+
code.push('curl %s %s', opts.short ? '-X' : '--request', source.method)
27+
.push(util.format('%s%s', opts.short ? '' : '--url ', helpers.quote(source.fullUrl)))
28+
29+
if (source.httpVersion === 'HTTP/1.0') {
30+
code.push(opts.short ? '-0' : '--http1.0')
31+
}
32+
33+
// construct headers
34+
Object.keys(source.headersObj).sort().forEach(function (key) {
35+
var header = util.format('%s: %s', key, source.headersObj[key])
36+
code.push('%s %s', opts.short ? '-H' : '--header', helpers.quote(header))
37+
})
38+
39+
if (source.allHeaders.cookie) {
40+
code.push('%s %s', opts.short ? '-b' : '--cookie', helpers.quote(source.allHeaders.cookie))
41+
}
42+
43+
// construct post params
44+
switch (source.postData.mimeType) {
45+
case 'multipart/form-data':
46+
source.postData.params.map(function (param) {
47+
var post = util.format('%s=%s', param.name, param.value)
48+
49+
if (param.fileName && !param.value) {
50+
post = util.format('%s=@%s', param.name, param.fileName)
51+
}
52+
53+
code.push('%s %s', opts.short ? '-F' : '--form', helpers.quote(post))
54+
})
55+
break
56+
57+
case 'application/x-www-form-urlencoded':
58+
if (source.postData.params) {
59+
source.postData.params.map(function (param) {
60+
code.push(
61+
'%s %s', opts.binary ? '--data-binary' : (opts.short ? '-d' : '--data'),
62+
helpers.quote(util.format('%s=%s', param.name, param.value))
63+
)
64+
})
65+
} else {
66+
code.push(
67+
'%s %s', opts.binary ? '--data-binary' : (opts.short ? '-d' : '--data'),
68+
helpers.escape(helpers.quote(source.postData.text))
69+
)
70+
}
71+
break
72+
73+
default:
74+
// raw request body
75+
if (source.postData.text && source.allHeaders['content-type'] === 'application/json') {
76+
code.push(
77+
'%s %s', opts.binary ? '--data-binary' : (opts.short ? '-d' : '--data'),
78+
helpers.quote(JSON.stringify(JSON.parse(source.postData.text), null, 2))
79+
)
80+
}
81+
}
82+
83+
return code.join()
84+
}
85+
86+
module.exports.info = {
87+
key: 'yapstonecurl',
88+
title: 'Yapstone cURL',
89+
link: 'http://curl.haxx.se/',
90+
description: 'cURL is a command line tool and library for transferring data with URL syntax'
91+
}

0 commit comments

Comments
 (0)