|
| 1 | +'use strict' |
| 2 | + |
| 3 | +var CodeBuilder = require('../../helpers/code-builder') |
| 4 | + |
| 5 | +module.exports = function (command) { |
| 6 | + return function (source, options) { |
| 7 | + var code = new CodeBuilder() |
| 8 | + var methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'] |
| 9 | + |
| 10 | + if (methods.indexOf(source.method.toUpperCase()) === -1) { |
| 11 | + return 'Method not supported' |
| 12 | + } |
| 13 | + |
| 14 | + var commandOptions = [] |
| 15 | + |
| 16 | + // Add headers, including the cookies |
| 17 | + var headers = Object.keys(source.headersObj) |
| 18 | + |
| 19 | + // construct headers |
| 20 | + if (headers.length) { |
| 21 | + code.push('$headers=@{}') |
| 22 | + headers.forEach(function (key) { |
| 23 | + if (key !== 'connection') { // Not allowed |
| 24 | + code.push('$headers.Add("%s", "%s")', key, source.headersObj[key]) |
| 25 | + } |
| 26 | + }) |
| 27 | + commandOptions.push('-Headers $headers') |
| 28 | + } |
| 29 | + |
| 30 | + // construct cookies |
| 31 | + if (source.cookies.length) { |
| 32 | + code.push('$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession') |
| 33 | + |
| 34 | + source.cookies.forEach(function (cookie) { |
| 35 | + code.push('$cookie = New-Object System.Net.Cookie') |
| 36 | + |
| 37 | + code.push("$cookie.Name = '%s'", cookie.name) |
| 38 | + code.push("$cookie.Value = '%s'", cookie.value) |
| 39 | + code.push("$cookie.Domain = '%s'", source.uriObj.host) |
| 40 | + |
| 41 | + code.push('$session.Cookies.Add($cookie)') |
| 42 | + }) |
| 43 | + commandOptions.push('-WebSession $session') |
| 44 | + } |
| 45 | + |
| 46 | + if (source.postData.text) { |
| 47 | + commandOptions.push("-ContentType '" + source.allHeaders['content-type'] + "'") |
| 48 | + commandOptions.push("-Body '" + source.postData.text + "'") |
| 49 | + } |
| 50 | + |
| 51 | + code.push("$response = %s -Uri '%s' -Method %s %s", command, source.fullUrl, source.method, commandOptions.join(' ')) |
| 52 | + return code.join() |
| 53 | + } |
| 54 | +} |
0 commit comments