how to recreate a working CURL command with Invoke-WebRequest in Powershell how to recreate a working CURL command with Invoke-WebRequest in Powershell powershell powershell

how to recreate a working CURL command with Invoke-WebRequest in Powershell


Try adding the parameter -ContentType e.g.:

Invoke-WebRequest -Headers @{"X-Api-Key" = "j65k423lj4k2l3fds"} -Method PUT `                  -Body "alerts_enabled=true" -Uri https://some/working/file.xml `                  -ContentType application/x-www-form-urlencoded

That results in a request that looks like this (from Fiddler):

PUT http://some/working/file.xml HTTP/1.1X-Api-Key: j65k423lj4k2l3fdsUser-Agent: Mozilla/5.0 (Windows NT; Windows NT 6.2; en-US) WindowsPowerShell/5.0.9701.0Content-Type: application/x-www-form-urlencodedHost: someContent-Length: 19Expect: 100-continuealerts_enabled=true

For testing, I changed the URL from https to http. If that doesn't work, go download Fiddler and inspect the RAW request when curl is used to see what is different.


got it to work natively using invoke-webrequest. powershell guru here at work helped me out. Switched to the New Relic API version 2 (available at https://rpm.newrelic.com/api/explore), which uses JSON instead of xml, and made some sytax tweaks.

$json = @"{"alert_policy":[{"enabled":"true"}]"@$headers = @{}$headers["X-Api-Key"] = "j65k423lj4k2l3fds"Invoke-WebRequest -Uri "https://some/working/file.json" -Body $json -ContentType "application/json" -Headers $headers -Method Post


This works for me in Powershell using the curl alias to Invoke-WebRequest...

 curl -H @{"X-Api-Key" = "j65k423lj4k2l3fds"} -Method PUT 'https://some/working/file.xml'