如何在Powershell中使用Invoke-WebRequest重新创建一个有效的CURL命令

栏目: 服务器 · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/23552704/how-to-recreate-a-working-curl-command-with-invoke-webrequest-in-powershell

此curl命令按预期工作:

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

如何使用Invoke-WebRequest在PS中本机重新创建?我试过了

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).

谢谢

尝试添加参数-ContentType,例如:

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

这导致一个看起来像这样的请求(来自Fiddler):

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

为了测试,我将URL从https更改为http.如果这不起作用,请下载Fiddler并在使用curl查看RAW请求时查看不同的内容.

翻译自:https://stackoverflow.com/questions/23552704/how-to-recreate-a-working-curl-command-with-invoke-webrequest-in-powershell


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Programming in Haskell

Programming in Haskell

Graham Hutton / Cambridge University Press / 2007-1-18 / GBP 34.99

Haskell is one of the leading languages for teaching functional programming, enabling students to write simpler and cleaner code, and to learn how to structure and reason about programs. This introduc......一起来看看 《Programming in Haskell》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

URL 编码/解码
URL 编码/解码

URL 编码/解码