Call REST API from PowerShell Script

后端 未结 3 1493
北恋
北恋 2020-12-24 01:29

How can I call a rest based API from a PowerShell script and process the Json answer?

3条回答
  •  失恋的感觉
    2020-12-24 01:49

    I created this Get-Http function to make HTTP requests

    param([string]$url)
    
    $req = [System.Net.WebRequest]::Create($url)
    $req.Method ="GET"
    $req.ContentLength = 0
    
    $resp = $req.GetResponse()
    $reader = new-object System.IO.StreamReader($resp.GetResponseStream())
    $reader.ReadToEnd()
    

    Dealing with the end result as xml is really easy, however, if you want to process JSON you probably will need some .Net library like JSON.Net.

提交回复
热议问题