cURL to Invoke-Webrequest command

◇◆丶佛笑我妖孽 提交于 2020-01-15 13:36:48

问题


Can anyone give me a hint, how to convert following curl command to PS Invoke-Webrequest ?

curl -d {\"password\":\"$password\"\} $vault/v1/auth/userpass/login/${login,,}

I have some thoughts on this, but cannot figure out, how to finish this:

$vault="3.3.3.3:8500"
$pair = (Get-Credential)
$params = ????
$login = $pair.getNetworkCredential().username
$pass = $pair.getNetworkCredential().password

Invoke-WebRequest $url -Method Post -Credential $pair -Body $params -UseBasicParsing

How to correctly pass $params like in curl request?


回答1:


the Params should be a hashtable @{}.

$Params = @{
    Username="TestName";
    Password="TestPassword";
    Places = @("Here","There");
}

Here is a example using a test API called jsonplaceholder

$url = 'https://jsonplaceholder.typicode.com/posts'

$params = @{
    title: 'foo'
    body: 'bar'
    userId:1
}

(Invoke-WebRequest $url -Method Post -Body $params -UseBasicParsing).content | ConvertFrom-JSON

Response

                                                     id
title: 'foo'                                       
body: 'bar'                                        
userId:1                                           

---------------------------------------------------  --
                                                    101


来源:https://stackoverflow.com/questions/54520054/curl-to-invoke-webrequest-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!