我目前正在研究在 PowerShell 中调用 ReST 请求的用例。POST 请求的主体是动态创建的,从 CSV 文件中读取数据。
这是我的最终请求正文应该是这样的
{
"@type": "mtTaskParameter",
"name": "$src_sfdc$",
"type": "EXTENDED_SOURCE",
"sourceConnectionId":"00002E0B00000000000C"
},
{
"@type": "mtTaskParameter",
"name": "$tgt_db_del$",
"type": "TARGET",
"targetConnectionId":"00002E0B00000000000D"
},
{
"@type": "mtTaskParameter",
"name": "$tgt_db_ups$",
"type": "TARGET",
"targetConnectionId":"00002E0B00000000000D"
},
{
"@type": "mtTaskParameter",
"name": "$tgt_status$",
"type": "TARGET",
"targetConnectionId":"00002E0B00000000000D"
}
}
目前我已经实现如下
if($connectionParameterized -eq "true"){
$str = @"
"@type": "mtTaskParameter",
"name": "$name",
"type": "$type"
"@
if($type -eq "SOURCE"){
$sourceConnectionId = <get source id>
$str = $str+
@"
,"sourceConnectionId":"$sourceConnectionId"
"@
}
if($type -eq "TARGET"){
$targetConnectionId = <get target id>
$str = $str+
@"
,"targetConnectionId":"$targetConnectionId"
"@
}
$finalstr = $finalstr+@"
{
$str
},
"@
}
这很好用,但是代码变得非常混乱并且难以扩展。同样在打印时,格式不正确。
有没有更好的方法来处理这个?
注意:从示例中可以看出,请求正文包含几个特殊字符,如 @、$ 等。