0

I have the following REST URL:

https://webserviceconsume.domain.local/web/api/rest?formatType=json&opName=userAPI&opData={param0:"sample",param1:{"list":[{"name1":"sample1","value1":"sample1"},{"name2":"sample2","value2":"sample2"}]},param2:-1}

I want to write a PowerShell script to call this URI. It should basically look like this:

$response = Invoke-RestMethod -Uri 'https://webserviceconsume.domain.local/web/api/rest' -Body $body -Headers $headers -ContentType "application/json"

Here's the header:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("accept", 'application/json')
$headers.Add("X-App-Key", 'XXXXX')

and then I want to build the $body but don't know how to do it.

0

3 Answers 3

1

Try the below variable as body.

$body = [PSCustomObject]@{
    param0 = 'sample';
    param1 =  @{
        list = @(
            @{name1 = "sample1"; value1 = "sample1"};
            @{name2 = "sample2"; value2 = "sample2"}
        )
    };
    param2 = -1
} | ConvertTo-Json -Depth 3
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but as i mentioned to Christy how to prepare the body part from this URL?
@rora, try the above variable. I hope it is the correct part of the url I took.
1

Using Rest with PowerShell is a very common thing, with many blog posts, articles and Youtube Videos explaining the topic and showing examples.

Simple Examples of PowerShell's Invoke-RestMethod

# Simple GET example

$response = Invoke-RestMethod 'http://example.com/api/people'
# assuming the response was in this format { "items": [] }
# we can now extract the child people like this
$people = $response.items

# GET with custom headers example

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("X-DATE", '9/29/2014')
$headers.Add("X-SIGNATURE", '234j123l4kl23j41l23k4j')
$headers.Add("X-API-KEY", 'testuser')

$response = Invoke-RestMethod 'http://example.com/api/people/1' -Headers $headers


# PUT/POST example

$person = @{
    first='joe'
    lastname='doe'
}
$json = $person | ConvertTo-Json
$response = Invoke-RestMethod 'http://example.com/api/people/1' -Method Put -Body $json -ContentType 'application/json'

# DELETE example

$response = Invoke-RestMethod 'http://example.com/api/people/1' -Method Delete

Comments

0

I've only been using PowerShell for 3 days now so this may not be the most efficient code but it's what is working for me

$URL        = ""

$Method     = 'PUT'

$Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Headers.Add("accept", 'application/json')
$Headers.Add("X-App-Key", 'XXXXX')

$Body       = ""

$Out        = ".\response.json"

    $response = 
    @{
        Uri         = $URL
        Method      = $Method
        Headers     = $Headers
        Body        = $Body
    }
    Invoke-RestMethod @response | ConvertTo-Json | Out-File $Out

1 Comment

Thanks but my main concern is preparing Body part from this URL and not the basic structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.