0

I'm currently trying to create a POST request using PowerShell. this is my initial 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" }) 

This is the function to call the API:

function Call-API { param ($ID, $criteria, $name, $description) $url = "$($global:apiURL)?id=1" $body =@{ criteria = $criteria; name = $name; description = $description; SpaceId = $ID; static = $false } (($body | ConvertTo-Json) -replace '"', '\"') try { $results = Invoke-RestMethod -Uri $url -Headers $global:headers -Body $body -Method Post -ContentType "application/json" return $results.results } catch { Show-Error -errorInfo $_ } 

Then I'm calling the function with this code:

$hashArrayOfStrings | ForEach-Object { try { Call-API -ID $ID -criteria $_.criteria -name $_.name -description $_.description } catch { Show-Error -errorInfo $_ } } 

Testing with 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"}' 

and did some testing with python using requests and also works.

I think the problem is escaping the characters. the API is expecting a string in the data of POST.

I've tested with ` in front of each double quote symbol with the same result:

Microsoft.PowerShell.Commands.HttpResponseException: Response status code does not indicate success: 405 (Method Not Allowed). at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord) 
2
  • 2
    In the 8th line of your first code block you have “” quotes and not "". Commented Nov 16, 2019 at 7:12
  • thank you for the edits @double-beep Commented Nov 18, 2019 at 22:44

1 Answer 1

1

Note: first of all I cannot test a solution because I cannot pass the query to a similar API, but after some tests the following seems to be you issue.

In Line 5 of you second block Call-API function you use -replace to escape ".

The ConvertTo-Json function in in PowerShell already escapes required chars in strings. Also note that PowerShell Handels non-converted JSON Objects as a string so doing -replace '"', '\"' will result as follows:

(@{name = '"name"'} | ConvertTo-Json) -replace '"', '\"'

output:

{\"name\": \"\\"name\\"\"}

which is not valid json. removing the -replace function outputs:

{"name": "\"name\""}

which is valid json

Apart from this you can use PowerShell splatting to make your function a easier to read/manage. Also if you are sure the passed hashtable is the same for the function just pass it as a whole and call the properties in the function.

$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 } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.