如何在Powershell中使用Invoke-WebRequest重新创建有效的CURL命令 [英] how to recreate a working CURL command with Invoke-WebRequest in Powershell

查看:55
本文介绍了如何在Powershell中使用Invoke-WebRequest重新创建有效的CURL命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此curl命令可根据需要工作:

This curl command works as desired:

curl -H "X-Api-Key:j65k423lj4k2l3fds" `
     -X PUT `
     -d "alerts_enabled=true" `
        https://some/working/file.xml

如何使用 Invoke-WebRequest 在PS中本机重新创建?我已经尝试

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 https://some/working/file.xml

我也尝试为所有参数制作对象(例如 $ headers = @ { X-Api-Key = Key:j65k423lj4k2l3fds } 并传递 -Headers $ headers )。

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

谢谢

推荐答案

让它可以使用invoke-webrequest在本地运行。在这里工作的powershell专家帮助了我。切换到新的Relic API版本2(可在 https://rpm.newrelic.com/api/explore ),它使用JSON而不是xml,并进行了一些语法调整。

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

这篇关于如何在Powershell中使用Invoke-WebRequest重新创建有效的CURL命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆