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

This curl command works as desired:

curl -H "X-Api-Key:j65k423lj4k2l3fds" ` -X PUT ` -d "alerts_enabled=true" ` 

How can I recreate this natively in PS with Invoke-WebRequest? I've tried

Invoke-WebRequest -Headers @{"X-Api-Key" = "j65k423lj4k2l3fds"} ` -Method PUT ` -Body "alerts_enabled=true" ` -Uri 

I've also tried making objects for all the params (e.g. $headers = @{"X-Api-Key" = "Key:j65k423lj4k2l3fds"} and passing -Headers $headers).

Thanks

0

3 Answers

Try adding the parameter -ContentType e.g.:

Invoke-WebRequest -Headers @{"X-Api-Key" = "j65k423lj4k2l3fds"} -Method PUT ` -Body "alerts_enabled=true" -Uri ` -ContentType application/x-www-form-urlencoded

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

PUT HTTP/1.1
X-Api-Key: j65k423lj4k2l3fds
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 6.2; en-US) WindowsPowerShell/5.0.9701.0
Content-Type: application/x-www-form-urlencoded
Host: some
Content-Length: 19
Expect: 100-continue
alerts_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.

1

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 ), 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 "" -Body $json -ContentType "application/json" -Headers $headers -Method Post
1

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

 curl -H @{"X-Api-Key" = "j65k423lj4k2l3fds"} -Method PUT '

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like