powershellにもcurlがあることを知りQiita apiを試してみた。
curlはInvoke-WebRequestのAliasなのでInvoke-WebRequestを試してみたといった方が正確なのだろう。
受け取ったJSON形式のデータはconvertfrom-jsonでPSCustomObjectの配列に変換する。
PS >get-command curl
CommandType Name Version Source
----------- ---- ------- ------
Alias curl -> Invoke-WebRequest
PS >$url = 'https://qiita.com/api/v2/items?page=1&per_page=10'
PS >$r = curl -URI $url
PS >$r | get-member
TypeName: Microsoft.PowerShell.Commands.HtmlWebResponseObject
Name MemberType Definition
---- ---------- ----------
Dispose Method void Dispose(), void IDisposable.Dispose()
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
AllElements Property Microsoft.PowerShell.Commands.WebCmdletElementCollection AllElements {get;}
BaseResponse Property System.Net.WebResponse BaseResponse {get;set;}
Content Property string Content {get;}
Forms Property Microsoft.PowerShell.Commands.FormObjectCollection Forms {get;}
Headers Property System.Collections.Generic.Dictionary[string,string] Headers {get;}
Images Property Microsoft.PowerShell.Commands.WebCmdletElementCollection Images {get;}
InputFields Property Microsoft.PowerShell.Commands.WebCmdletElementCollection InputFields {get;}
Links Property Microsoft.PowerShell.Commands.WebCmdletElementCollection Links {get;}
ParsedHtml Property mshtml.IHTMLDocument2 ParsedHtml {get;}
RawContent Property string RawContent {get;set;}
RawContentLength Property long RawContentLength {get;}
RawContentStream Property System.IO.MemoryStream RawContentStream {get;}
Scripts Property Microsoft.PowerShell.Commands.WebCmdletElementCollection Scripts {get;}
StatusCode Property int StatusCode {get;}
StatusDescription Property string StatusDescription {get;}
PS >$c = ($r.content | convertfrom-json )
PS >$i = 0
PS >foreach ( $a in $c ){ Write-Host "[${i}]" ${a}.title; $i ++ }
scriptにしておく。
qiita.ps1
$url = 'https://qiita.com/api/v2/items?page=1&per_page=20'
$r = curl -URI $url
$c = ($r.content | convertfrom-json )
$i = 0
foreach ( $a in $c ){
Write-Host "[${i}]" ${a}.title
$i ++
}
apiの利用には「1時間に60回まで」など制限事項があるので注意方。
※2022-10-26 どうにかmarkdown?に変更。