Created
November 25, 2020 21:03
-
-
Save markekraus/a0135d0a5f52b609eda513df4db71cab to your computer and use it in GitHub Desktop.
Example using HttpClient in PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create single HttpClient | |
$client = [System.Net.Http.HttpClient]::new() | |
# Perform multiple GETs | |
foreach ($url in $urls) { | |
$clientResult = $client.GetStringAsync($url). | |
GetAwaiter(). | |
GetResult() | |
$clientResult | ConvertFrom-Json | |
} | |
# Performing a POST | |
$body = @{ | |
foo = 'bar' | |
baz = 'qux' | |
} | ConvertTo-Json | |
$content = [System.Net.Http.HttpRequestMessage]::new() | |
$content.Headers.Add('Accept','application/json') | |
$content.Content = [System.Net.Http.StringContent]::new( | |
$body, | |
[System.Text.Encoding]::UTF8,'application/json') | |
$content.Method = 'POST' | |
$content.RequestUri = $url | |
$clientResultMessage = $client.SendAsync($content). | |
GetAwaiter(). | |
GetResult() | |
$result = $clientResultMessage. | |
Content. | |
ReadAsStringAsync(). | |
GetAwaiter(). | |
GetResult() | |
$result | ConvertFrom-Json |
This is helpful, thanks
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @markekraus!