Я искал что-то похожее и выложил это http://amonkeysden.tumblr.com/post/5842982257/posting-to-a-restful-api-with-powershell - там также есть ссылка на источник, но более конкретно это модуль:
https://bitbucket.org/thompsonson/powershellmodulerepository/src/5e0afe9d0e26/PsUrl/PsUrl.psm1
и вам, вероятно, нужна функция New-RestItem
, хотя Write-URL
может сделать это и для вас.
Вы должны передать JSON в виде строки - пример ниже не тестируется...:).
напр.
New-RestItem <a href="http://www.tumblr.com/api/write" rel="nofollow">http://www.tumblr.com/api/write</a> -Data @{"JSON" = '{"something": "value", "more": {"morekey1":"value", "morekey2": "value"} }'
function New-RestItem {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, Position=0)]
[String]$Url,
[HashTable]$Data,
[TimeSpan]$Timeout = [System.TimeSpan]::FromMinutes(1)
)
Add-Type -AssemblyName System.Web
$formData = [System.Web.HttpUtility]::ParseQueryString("")
foreach($key in $Data.Keys){
$formData.Add($key, $Data[$key])
}
Write-Verbose $formData.ToString()
$reqBody = [System.Text.Encoding]::Default.GetBytes($formData.ToString())
try{
$req = [System.Net.WebRequest]::Create($Url)
$req.Method = "POST"
$req.ContentType = "application/x-www-form-urlencoded"
$req.Timeout = $Timeout.TotalMilliseconds
$reqStream = $req.GetRequestStream()
$reqStream.Write($reqBody, 0, $reqBody.Length)
$reqStream.Close()
$resp = $req.GetResponse()
Write-Verbose $resp
Write-Output $resp.StatusCode
}
catch [System.Net.WebException]{
if ($_.Exception -ne $null -and $_.Exception.Response -ne $null) {
$errorResult = $_.Exception.Response.GetResponseStream()
$errorText = (New-Object System.IO.StreamReader($errorResult)).ReadToEnd()
Write-Warning "The remote server response: $errorText"
Write-Output $_.Exception.Response.StatusCode
} else {
throw $_
}
}
<#
.Synopsis
POSTs the data to the given URL
.Description
POSTs the data to the given URL and returns the status code
.Parameter Url
URL to POST
.Parameter Data
Hashtable of the data to post.
.Parameter Timeout
Optional timeout value, by default timeout is 1 minute.
.Input
URL and a hash of the data to create
.Output
The HTTP Status code (Created (201), Forbidden (403) or Bad Request (400))
.Example
PS:> New-RestItem http://www.tumblr.com/api/write -Data @{"Foo" = "Bar" }
Description
-----------
POST's data to the tumblr write API as application/x-www-form-urlencoded
#>
}