liv*_*arn 2 rest powershell powershell-5.0
我正在使用PowerShell尝试POST请求.它需要原始的身体.我知道如何使用PowerShell传递表单数据,但对rawdata类型不确定.对于Postman中的简单原始数据,例如
{
"@type":"login",
"username":"xxx@gmail.com",
"password":"yyy"
}
Run Code Online (Sandbox Code Playgroud)
我在PowerShell中传递下面,它工作正常.
$rawcreds = @{
'@type' = 'login'
username=$Username
password=$Password
}
$json = $rawcreds | ConvertTo-Json
Run Code Online (Sandbox Code Playgroud)
但是,对于像下面这样复杂的原始数据,我不确定如何传入PowerShell.
{
"@type": Sample_name_01",
"agentId": "00000Y08000000000004",
"parameters": [
{
"@type": "TaskParameter",
"name": "$source$",
"type": "EXTENDED_SOURCE"
},
{
"@type": "TaskParameter",
"name": "$target$",
"type": "TARGET",
"targetConnectionId": "00000Y0B000000000020",
"targetObject": "sample_object"
}
],
"mappingId": "00000Y1700000000000A"
}
Run Code Online (Sandbox Code Playgroud)
我的解释是你的第二个代码块是你想要的原始JSON,而你不确定如何构造它.最简单的方法是使用here字符串:
$body = @"
{
"@type": Sample_name_01",
"agentId": "00000Y08000000000004",
"parameters": [
{
"@type": "TaskParameter",
"name": "$source$",
"type": "EXTENDED_SOURCE"
},
{
"@type": "TaskParameter",
"name": "$target$",
"type": "TARGET",
"targetConnectionId": "00000Y0B000000000020",
"targetObject": "sample_object"
}
],
"mappingId": "00000Y1700000000000A"
}
"@
Invoke-WebRequest -Body $body
Run Code Online (Sandbox Code Playgroud)
变量替换有效(因为我们使用@"而不是@'),但你不必对文字"字符进行杂乱的转义.
所以这意味着$source$它将被解释为一个名为$source嵌入字符串后跟文字的变量$.如果那不是你想要的(也就是说,如果你想在字体中使用$source$),那么使用@'和'@包含你的here字符串,以便不嵌入powershell变量.
| 归档时间: |
|
| 查看次数: |
4160 次 |
| 最近记录: |