Skip to content

Commit 7496355

Browse files
feat(powershell): add powershell webrequest target (Kong#127)
Closes Kong#105
1 parent cb28c98 commit 7496355

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

src/targets/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
objc: require('./objc'),
1212
ocaml: require('./ocaml'),
1313
php: require('./php'),
14+
powershell: require('./powershell'),
1415
python: require('./python'),
1516
ruby: require('./ruby'),
1617
shell: require('./shell'),

src/targets/powershell/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
module.exports = {
4+
info: {
5+
key: 'powershell',
6+
title: 'Powershell',
7+
extname: '.ps1',
8+
default: 'webrequest'
9+
},
10+
11+
webrequest: require('./webrequest')
12+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict'
2+
3+
var CodeBuilder = require('../../helpers/code-builder')
4+
5+
module.exports = function (source, options) {
6+
var code = new CodeBuilder()
7+
var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS' ]
8+
9+
if (methods.indexOf(source.method.toUpperCase()) === -1) {
10+
return 'Method not supported'
11+
}
12+
13+
var commandOptions = []
14+
15+
// Add headers, including the cookies
16+
var headers = Object.keys(source.headersObj)
17+
18+
// construct headers
19+
if (headers.length) {
20+
code.push('$headers=@{}')
21+
headers.forEach(function (key) {
22+
if (key !== 'connection') { // Not allowed
23+
code.push('$headers.Add("%s", "%s")', key, source.headersObj[key])
24+
}
25+
})
26+
commandOptions.push('-Headers $headers')
27+
}
28+
29+
// construct cookies
30+
if (source.cookies.length) {
31+
code.push('$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession')
32+
33+
source.cookies.forEach(function (cookie) {
34+
code.push('$cookie = New-Object System.Net.Cookie')
35+
36+
code.push("$cookie.Name = '%s'", cookie.name)
37+
code.push("$cookie.Value = '%s'", cookie.value)
38+
code.push("$cookie.Domain = '%s'", source.uriObj.host)
39+
40+
code.push('$session.Cookies.Add($cookie)')
41+
})
42+
commandOptions.push('-WebSession $session')
43+
}
44+
45+
if (source.postData.text) {
46+
commandOptions.push("-ContentType '" + source.allHeaders['content-type'] + "'")
47+
commandOptions.push("-Body '" + source.postData.text + "'")
48+
}
49+
50+
code.push("$response = Invoke-WebRequest -Uri '%s' -Method %s %s", source.fullUrl, source.method, commandOptions.join(' '))
51+
return code.join()
52+
}
53+
54+
module.exports.info = {
55+
key: 'webrequest',
56+
title: 'Invoke-WebRequest',
57+
link: 'https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-WebRequest',
58+
description: 'Powershell Invoke-WebRequest client'
59+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict'
2+
3+
require('should')
4+
5+
module.exports = function (snippet, fixtures) {}

0 commit comments

Comments
 (0)