如何使用 PowerShell 使用 Web 服务?
How to consume a web service using PowerShell?
我有以下 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}
我想编写一个 PowerShell 脚本来调用这个 URI。它基本上应该是这样的:
$response = Invoke-RestMethod -Uri 'https://webserviceconsume.domain.local/web/api/rest' -Body $body -Headers $headers -ContentType "application/json"
这是header:
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("accept", 'application/json')
$headers.Add("X-App-Key", 'XXXXX')
然后我想构建 $body
但不知道该怎么做。
我现在只使用 PowerShell 3 天,所以这可能不是最有效的代码,但它对我有用
$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
试试下面的变量作为正文。
$body = [PSCustomObject]@{
param0 = 'sample';
param1 = @{
list = @(
@{name1 = "sample1"; value1 = "sample1"};
@{name2 = "sample2"; value2 = "sample2"}
)
};
param2 = -1
} | ConvertTo-Json -Depth 3
将 Rest 与 PowerShell 结合使用是很常见的事情,with many blog posts, articles and Youtube Videos 解释主题并展示示例。
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
我有以下 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}
我想编写一个 PowerShell 脚本来调用这个 URI。它基本上应该是这样的:
$response = Invoke-RestMethod -Uri 'https://webserviceconsume.domain.local/web/api/rest' -Body $body -Headers $headers -ContentType "application/json"
这是header:
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("accept", 'application/json')
$headers.Add("X-App-Key", 'XXXXX')
然后我想构建 $body
但不知道该怎么做。
我现在只使用 PowerShell 3 天,所以这可能不是最有效的代码,但它对我有用
$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
试试下面的变量作为正文。
$body = [PSCustomObject]@{
param0 = 'sample';
param1 = @{
list = @(
@{name1 = "sample1"; value1 = "sample1"};
@{name2 = "sample2"; value2 = "sample2"}
)
};
param2 = -1
} | ConvertTo-Json -Depth 3
将 Rest 与 PowerShell 结合使用是很常见的事情,with many blog posts, articles and Youtube Videos 解释主题并展示示例。
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