这是我在Powershell中进行发布请求的第一时间。我有一个很大的请求,其中包含很多变量,执行时出现一些错误。
这是我的公式
function PostCustomerUpdate($cust){
$phone = -join($cust.AreaCode,$cust.Phone)
Invoke-WebRequest -Uri myAPIURL -ContentType "application/json" -Method POST -Body {
"Options": 2,
"ModifiedCustomer": {
"CustomerID": $cust.Id,
"ADObjectGuid": $cust.ADGUID,
"AppID": $cust.AppID,
"AppCustomerID": $cust.AppCustomerID,
"OriginatingSystemID": $cust.OriginatingSystemID,
"OriginatingSystemDescription": $cust.OriginatingSystemDescription,
"Action": $cust.Action,
"Title": $cust.Title,
"Suffix": $cust.Suffix,
"Gender": $cust.Gender,
"FirstName": $cust.FirstName,
"LastName": $cust.LastName,
"CustomerTypeDescription": $cust.CustomerTypeDescription,
"CustomerStatusTypeExtendedDescription": $cust.CustomerStatusTypeExtendedDescription,
"CustomerStatusTypeDescription": $cust.CustomerStatusTypeDescription,
"CustomerStatusReasonTypeDescription": $cust.CustomerStatusReasonTypeDescription,
"PreferredLanguage": $cust.PreferredLanguage,
"Applications":[
{
"ApplicationCustomerLoginId": $cust.EmailAddress,
"ApplicationCustomerID": $phone,
"Action": 1,
}
]
}
}
}
尝试运行时出现以下错误
At line:45 char:14
+ "Options": 2,
+ ~
Unexpected token ':' in expression or statement.
At line:47 char:21
+ "CustomerID": $cust.Id,
+ ~
Unexpected token ':' in expression or statement.
At line:66 char:35
+ "ApplicationCustomerLoginId": $cust.EmailAddress,
+ ~
Unexpected token ':' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
我已经尝试了几乎所有我知道的东西,但我很茫然。
答案 0 :(得分:1)
您可以使用哈希表和列表使用PSObjects创建JSON主体。可以使用@()
在PowerShell中复制JSON中的列表,并使用@{}
完成字典。最后,您可以使用ConvertTo-JSON
进行转换,但这不是强制性的。
$body = [PSCustomObject] @{
Options = 2
...
ModifiedCustomer = @{
CustomerID = $cust.Id
ADObjectGuid = $cust.ADGUID
AppID = $cust.AppID
Applications = @(
@{
ApplicationCustomerLoginId = $cust.EmailAddress
ApplicationCustomerID = $phone
Action = 1
}
)
}
} | ConvertTo-Json -Depth 3
您可以照常使用WebRequest。
Invoke-WebRequest -Uri myAPIURL -ContentType "application/json" -Method POST -Body $body
答案 1 :(得分:0)
对于PowerShell,在通过解释器或转换器发送之前,JSON只是一个字符串。由于您的值以{
开头而不是表示字符串的内容,因此当解析器不知道如何处理单独的:
时,您最终会收到错误消息。 {
表示脚本块的开头,这是另一种对象类型。
我建议使用here-string(双引号版本)将JSON括起来,以保留行格式并允许变量插值。
function PostCustomerUpdate($cust){
$phone = -join($cust.AreaCode,$cust.Phone)
$json = @"
{
"Options": 2,
"ModifiedCustomer": {
"CustomerID": $cust.Id,
"ADObjectGuid": $cust.ADGUID,
"AppID": $cust.AppID,
"AppCustomerID": $cust.AppCustomerID,
"OriginatingSystemID": $cust.OriginatingSystemID,
"OriginatingSystemDescription": $cust.OriginatingSystemDescription,
"Action": $cust.Action,
"Title": $cust.Title,
"Suffix": $cust.Suffix,
"Gender": $cust.Gender,
"FirstName": $cust.FirstName,
"LastName": $cust.LastName,
"CustomerTypeDescription": $cust.CustomerTypeDescription,
"CustomerStatusTypeExtendedDescription": $cust.CustomerStatusTypeExtendedDescription,
"CustomerStatusTypeDescription": $cust.CustomerStatusTypeDescription,
"CustomerStatusReasonTypeDescription": $cust.CustomerStatusReasonTypeDescription,
"PreferredLanguage": $cust.PreferredLanguage,
"Applications":[
{
"ApplicationCustomerLoginId": $cust.EmailAddress,
"ApplicationCustomerID": $phone,
"Action": 1,
}
]
}
}
"@
Invoke-WebRequest -Uri myAPIURL -ContentType "application/json" -Method POST -Body $json
}