好的,我再次努力将cURL转换为PowerShell,特别是在哈希表结构中。这次哈希表在参数表中有多个项目:
这是Postman的cURL,可以正常工作:
curl -X POST \
https://example.net \
-H 'Authorization: Bearer 1234567890' \
-H 'Content-Type: application/json' \
-d '{
"data": [
{
"DUID": 3299,
"AID": 551,
"CID": 10002,
"Parameters": [
{
"name": "Customer.*/Address1",
"value": "Street 1"
},
{
"name": "Customer.*/Address2",
"value": "Street 2"
},
{
"name": "Customer.*/City",
"value": "Somewhere"
},
{
"name": "Customer.*/State",
"value": "NSW"
},
{
"name": "Customer.*/PostCode",
"value": "1234"
}
]
}
]
}'
我已使用本文中的示例尝试了多种尝试:
cURL to PowerShell - Double hash table in --data?
这是我当前的脚本,但是PowerShell不喜欢参数表中各项之间的右花括号,逗号和左花括号:
$URL1 = "https://example.net"
$Body = @{
data = , @{
DUID= 3299
AID= 551
CID= 10002
Parameters = @{
name = "Customer.*/Address1"
value = "Street 1"
},
{
name = "Customer.*/Address2"
value = "Street 2"
},
{
name = "Customer.*/City"
value = "Somewhere"
},
{
name = "Customer.*/State"
value = "NSW"
},
{
name = "Customer.*/PostCode"
value = "2345"
}}}
$CurlArgument = '-s', '-X', 'POST',
'-H', 'Content-Type: application/json',
$URL1,
'-H',
$AuthBearer,
'-d',
($Body | ConvertTo-Json -Depth 4) -replace '"', '\"'
Write-Host "Command took" (Measure-Command {& $CURLEXE @CurlArgument}).TotalSeconds "Seconds" -ForegroundColor Yellow
我尝试取出多余的花括号,在花括号前添加@,取出逗号,但哈希表格式不正确,并且cURL请求失败。由于局限性,添加参数后无法对其进行修补,因此必须通过一条命令发送以上内容。
任何帮助(再次)将不胜感激。我曾尝试过Google并阅读有关哈希表的信息,但我不喜欢任何帮助。
当我当前查看$ CurlArgument时,它看起来像这样(这是错误的):
{
\"data\": [
{
\"CID\": 10002,
\"DUID\": 3299,
\"AID\": 551,
\"Parameters\": [
{
\"value\": \"Street 1\",
\"name\": \"Customer.*/Address1\"
},
{
\"Attributes\": \"\",
\"File\": null,
\"IsFilter\": false,
\"IsConfiguration\": false,
\"Module\": null,
\"StartPosition\": \"System.Management.Automation.PSToken\",
\"DebuggerHidden\": false,
\"Id\": \"1234567890\",
\"Ast\": \"{\r\n name = \\"Customer.*/Address2\\"\r\n value = \\"Street 1\\"\r\n }\"
},
... snip
答案 多亏了Mike Twc,我编辑了脚本,并且脚本有效
$URL1 = "https://example.net"
$Body = @{
data = , @{
DUID= 3299
AID= 551
CID= 10002
Parameters = @{
name = "Customer.*/Address1"
value = "Street 1"
},
@{
name = "Customer.*/Address2"
value = "Street 2"
},
@{
name = "Customer.*/City"
value = "Somewhere"
},
@{
name = "Customer.*/State"
value = "NSW"
},
@{
name = "Customer.*/PostCode"
value = "2345"
}}}
$CurlArgument = '-s', '-X', 'POST',
'-H', 'Content-Type: application/json',
$URL1,
'-H',
$AuthBearer,
'-d',
($Body | ConvertTo-Json -Depth 4) -replace '"', '\"'
Write-Host "Command took" (Measure-Command {& $CURLEXE @CurlArgument}).TotalSeconds "Seconds" -ForegroundColor Yellow
答案 0 :(得分:1)
您的哈希表已损坏,“参数”是一个脚本块数组(您错过了@)。 无论如何,我看不到使用curl的意义,为什么不这样做:
$url = "https://example.net"
$headers = @{'Authorization: Bearer'='1234567890'; 'Content-Type'='application/json'}
$body = '{your json here}'
Invoke-WebRequest -Uri $url -Method Post -Headers $headers -Body $body
关于损坏的哈希表,这对我来说效果很好:
$Body = @{
data = @{
DUID = 3299
AID = 551
CID = 10002
Parameters =
@{ name = "Customer.*/Address1"
value = "Street 1"},
@{
name = "Customer.*/Address2"
value = "Street 2"
},
@{
name = "Customer.*/City"
value = "Somewhere"
},
@{
name = "Customer.*/State"
value = "NSW"
},
@{
name = "Customer.*/PostCode"
value = "2345"
}
}
}
$Body | convertto-json -Depth 4