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

This topic contains 10 replies, has 2 voices, and was last updated by Profile photo of Pravakar Panigrahi Pravakar Panigrahi 6 months, 3 weeks ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #35848
    Profile photo of Pravakar Panigrahi
    Pravakar Panigrahi
    Participant

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

    {"@type":"login",
    "username":"[email protected]",
    "password":"yyy"
    }

    #35849
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Have you tried this?

    Invoke-WebRequest -Uri $yourUri -Method Post -Body @{
        [email protected]' = 'login'
        username = [email protected]'
        password = 'yyy'
    }
    

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

    #35855
    Profile photo of Pravakar Panigrahi
    Pravakar Panigrahi
    Participant

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

    #35893
    Profile photo of Pravakar Panigrahi
    Pravakar Panigrahi
    Participant

    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!

    #35894
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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

    #35898
    Profile photo of Pravakar Panigrahi
    Pravakar Panigrahi
    Participant

    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 = @{
                        [email protected]' = '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?

    #35900
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

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

    #35901
    Profile photo of Pravakar Panigrahi
    Pravakar Panigrahi
    Participant

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

    #35903
    Profile photo of Pravakar Panigrahi
    Pravakar Panigrahi
    Participant

    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?

    #35918
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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.
    
    #35921
    Profile photo of Pravakar Panigrahi
    Pravakar Panigrahi
    Participant

    This helped. Thank you!

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.