Search code examples
powershellcurlinvoke-webrequest

cURL to Invoke-Webrequest command


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?


Solution

  • 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