How to make a POST request using Powershell if body have a parameter @type

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


Have you tried this?

Invoke-WebRequest -Uri $yourUri -Method Post -Body @{
    '@type' = 'login'
    username = ''
    password = 'yyy'

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

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

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!

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:

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'

        $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?

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

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

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?

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'] = $
        # not sure if you want to take username / password out of your headers at this point, but you get the idea.

This helped. Thank you!