Author Posts

March 1, 2016 at 5:44 am

I want to make a POST request in PowerShell. Following is the body details in Postman.

{"@type":"login",
"username":"xxx@gmail.com",
"password":"yyy"
}

March 1, 2016 at 5:45 am

Have you tried this?

Invoke-WebRequest -Uri $yourUri -Method Post -Body @{
    '@type' = 'login'
    username = 'xxx@gmail.com'
    password = 'yyy'
}

I don't know if this will work or not; it would just be what I tried first.

March 1, 2016 at 7:28 am

This worked. Thank you very much for the help!!

March 1, 2016 at 6:56 pm

Can you also please guide me to any blogs/books covering ReST usage in PowerShell in more details. Basically I am looking to understand various aspects such as how to capture the response and store a particular value say session-id & use it in next request etc.

I have previously worked in java & this is my first attempt at PowerShell. Thanks so much in advance!

March 1, 2016 at 7:18 pm

For the most part, you don't have to worry about that. Invoke-WebRequest and Invoke-RestMethod both have a pair of parameters (-SessionVariable and -WebSession) which will handle keep track of session IDs and other cookies for you. It works like this:

# For the first request you make to establish your session:

Invoke-WebRequest -Uri $someUri -SessionVariable mySession # note there's no $ symbol here.

# For the remaining requests:

Invoke-WebRequest -Uri $someOtherUri -WebSession $mySession # here you use the $

There are examples of this in the help for Invoke-WebRequest, which show how you can use them to accomplish forms-based authentication to Facebook: https://technet.microsoft.com/en-us/library/hh849901.aspx

March 1, 2016 at 8:11 pm

That's very helpful. Will go through the links and understand the details.

One last query. For the previous code, I just modified to take the user credentials from a csv file and make the ReST requests for each set of data. Below is the code.


$Username = @()
$Password = @()

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept-Charset", 'UTF-8')
$headers.Add("Accept", 'application/json')

$usercreds = Import-Csv C:\work\PowerShell\login.csv ","

ForEach($row in $usercreds) {

        $Username = $($row.userid)
        $Password = $($row.password)
               
        Write-Host $Username
        Write-Host $Password

     
        $rawcreds = @{
                    '@type' = 'login'
                     username=$Username
                     password=$Password
        }

        $json = $rawcreds | ConvertTo-Json
        Invoke-RestMethod "my-custom-url" -Headers $headers -Method POST -Body $json -ContentType "application/json" -OutFile output.json
}

But I am getting 403 forbidden error. Anything that I missed?

March 1, 2016 at 8:23 pm

No idea. That error comes from your web server, and I don't know what it is you're trying to access.

March 1, 2016 at 8:37 pm

Oh ok. My query was more from syntactical point of view.

March 1, 2016 at 9:15 pm

Hi Dave, I went through the details but still couldn't resolve this.

My first request gives the following response.


"@type": "user",
"id": "00000703000000000010",
"orgId": "000007",

I need to save the value of id and add it to the header of the next request. Can you please let me know how I can achieve this?

March 2, 2016 at 5:17 am

Oh. That's annoying, but ok. Guess the API doesn't want to use sessions / cookies. You'll want to assign the result of Invoke-RestMethod to a variable, then add those values to your headers table. Probably something like this:

        $json = $rawcreds | ConvertTo-Json
        $result = Invoke-RestMethod "my-custom-url" -Headers $headers -Method POST -Body $json -ContentType "application/json" -OutFile output.json
        
        $headers['id'] = $result.id
        
        # not sure if you want to take username / password out of your headers at this point, but you get the idea.

March 2, 2016 at 5:38 am

This helped. Thank you!