how to convert this curl command to PowerShell

255 Views Asked by At

I am using PowerShell and want to convert this curl command in PowerShell. It works well in bash

curl -s -H 'X-AUTH-TOKEN:'09f4a92e8fbfc9f9f530771ac32d37a1'' -H "Content-Type: application/json" -X GET 'http://127.0.0.1/centreon/api/beta/monitoring/services?search=\{"service.state":"2","service.is_acknowledged":"0"\}'

Thanks for your help

2

There are 2 best solutions below

2
Cpt.Whale On

Additional headers can be added to the -Headers property as hash table entries:

$result = curl -Uri $uri -ContentType application/json -Method Get -Headers @{'X-AUTH-TOKEN'='09f4a92e8fbfc9f9f530771ac32d37a1'} 

You may want to try converting your uri string to type [System.URI]. This handles standard character replacement like ' ' to %20. I think curl does this already, but it may help you see if something is invalid:

$uri = [System.Uri]::new('http://127.0.0.1/centreon/api/beta/monitoring/services?search=\{"service.state":"2","service.is_acknowledged":"0"\}')

# check the output:
$uri.AbsoluteUri

http://127.0.0.1/centreon/api/beta/monitoring/services?search=%5C%7B%22service.state%22:%222%22,%22service.is_acknowledged%22:%220%22%5C%7D
1
Adolf Ku On

Here is the solution which worked for me. i have Changed {"service.state":"2","service.is_acknowledged":"0"\}to ==> {"service.state":"2","service.is_acknowledged":"0"}"

Thank you