使用invoke restmethod的powershell POST数据存在问题

使用invoke restmethod的powershell POST数据存在问题,powershell,http-post,Powershell,Http Post,我目前正在尝试使用PowerShell创建POST请求。这是我的首字母hashArray: $hashArrayOfStrings = @( @{ criteria = '"platform" = "iOS" AND "connected_at" >= "now-1d" AND "client.connected" >= "now-1d" AND "apns" = true AND ( "version" STARTS WITH "1" OR "version" STA

我目前正在尝试使用PowerShell创建
POST
请求。这是我的首字母
hashArray

$hashArrayOfStrings = @(
@{
    criteria    = '"platform" = "iOS" AND "connected_at" >= "now-1d" AND "client.connected" >= "now-1d" AND "apns" = true AND ( "version" STARTS WITH "1" OR "version" STARTS WITH "2" )'
    name        = "iOS"
    description = "Description1"
},
@{
    criteria    = '“platform” = “android” AND “connected_at” >= “now-1d” AND ( “client.connected” < “now-1d” OR “apns” = false ) AND ( “version” STARTS WITH “1” OR “version” STARTS WITH “2” )'
    name        = "name2"
    description = "Description2"
})
然后我用以下代码调用函数:

$hashArrayOfStrings | ForEach-Object {
                try {
                    Call-API -ID $ID -criteria $_.criteria -name $_.name -description $_.description
                }
                catch { Show-Error -errorInfo $_ }
            }
使用cURL works进行测试:

curl -X POST -H 'Authorization: Basic xxxxxxxxxxxx' -H "Content-Type: application/json" 'https://apiurl.com?Id=1' --data-binary '{"name": "test","description": "test description.","SpaceId": 1,"static": false,"criteria": "(\"platform\"=\"Android\") AND \"apns\"=false"}'
并使用请求对python进行了一些测试,也可以正常工作

我认为问题在于逃避角色。API在POST的数据中需要一个字符串

我在每个双引号符号前面使用了`进行了测试,结果相同:

Microsoft.PowerShell.Commands.HttpResponseException: Response status code does not indicate success: 405 (Method Not Allowed).
   at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)

注意:首先,我无法测试解决方案,因为我无法将查询传递给类似的API,但在一些测试之后,您似乎遇到了以下问题

在第二个块的第5行
调用API
函数中,您使用
-replace
来转义

PowerShell中的
ConvertTo Json
函数已在字符串中转义所需的字符。还请注意,PowerShell将未转换的Json对象作为字符串处理,因此执行
-replace','\'
将导致以下结果:

(@{name=''name'}转换为Json)-替换'''','\''

输出:

{\'name\':\'name\\'}

这不是有效的json。 卸下
-更换
功能输出:

{“name”:“\“name\”}

哪个是有效的json

除此之外,您还可以使用它使函数更易于阅读/管理。此外,如果您确定传递的哈希表与函数相同,只需将其作为一个整体传递并调用函数中的属性即可

$hashArrayOfStrings = @(
    @{
        criteria    = '"platform" = "iOS" AND "connected_at" >= "now-1d" AND "client.connected" >= "now-1d" AND "apns" = true AND ( "version" STARTS WITH "1" OR "version" STARTS WITH "2" )'
        name        = "iOS"
        description = "Description1"
    },
    @{
        criteria    = '“platform” = “android” AND “connected_at” >= “now-1d” AND ( “client.connected” < “now-1d” OR “apns” = false ) AND ( “version” STARTS WITH “1” OR “version” STARTS WITH “2” )'
        name        = "name2"
        description = "Description2"
    }
)

function Call-API($Id, $Object){
    $params = @{
        Uri     = "$($Global:apiURL)?id=1"
        Headers = $global:headers
        Method  = "POST"
        ContentType = "application/json" 
        Body    = @{
            SpaceId         = $Id
            static          = $false
            criteria        = $Object.critera
            name            = $Object.name
            $description    = $Object.description
        } | ConvertTo-Json
    }

    try{
        $Respose = Invoke-RestMethod @params
        return $Respose.results
    } catch {
        throw $_
    }
}

foreach($object in $hashArrayOfStrings){
    Call-API -Id $id -Object $object
    #you dont need a additional try catch here as this is embedded in your Call API function
}
$HashArrayOfString=@(
@{
标准='“平台”=“iOS”和“已连接”>=“now-1d”和“client.connected”>=“now-1d”和“apns”=真实且(“版本”以“1”开头,或“版本”以“2”开头)
name=“iOS”
description=“Description1”
},
@{
标准='“平台”=“android”和“连接”>“now-1d”和(“客户端连接”<“now-1d”或“apns”=错误)和(“版本”以“1”开头,或“版本”以“2”开头)
name=“name2”
description=“Description2”
}
)
函数调用API($Id$Object){
$params=@{
Uri=“$($Global:apiURL)?id=1”
Headers=$global:Headers
Method=“POST”
ContentType=“应用程序/json”
正文=@{
SpaceId=$Id
静态=$false
条件=$Object.critera
name=$Object.name
$description=$Object.description
}|转换为Json
}
试一试{
$Respose=Invoke RestMethod@params
返回$Respose.results
}抓住{
扔$_
}
}
foreach($HashArrayOfString中的对象){
调用API-Id$Id-Object$Object
#这里不需要额外的try-catch,因为它嵌入在调用API函数中
}

在第一个代码块的第8行中,您有
引号,而不是
。感谢您的编辑@double beep
$hashArrayOfStrings = @(
    @{
        criteria    = '"platform" = "iOS" AND "connected_at" >= "now-1d" AND "client.connected" >= "now-1d" AND "apns" = true AND ( "version" STARTS WITH "1" OR "version" STARTS WITH "2" )'
        name        = "iOS"
        description = "Description1"
    },
    @{
        criteria    = '“platform” = “android” AND “connected_at” >= “now-1d” AND ( “client.connected” < “now-1d” OR “apns” = false ) AND ( “version” STARTS WITH “1” OR “version” STARTS WITH “2” )'
        name        = "name2"
        description = "Description2"
    }
)

function Call-API($Id, $Object){
    $params = @{
        Uri     = "$($Global:apiURL)?id=1"
        Headers = $global:headers
        Method  = "POST"
        ContentType = "application/json" 
        Body    = @{
            SpaceId         = $Id
            static          = $false
            criteria        = $Object.critera
            name            = $Object.name
            $description    = $Object.description
        } | ConvertTo-Json
    }

    try{
        $Respose = Invoke-RestMethod @params
        return $Respose.results
    } catch {
        throw $_
    }
}

foreach($object in $hashArrayOfStrings){
    Call-API -Id $id -Object $object
    #you dont need a additional try catch here as this is embedded in your Call API function
}