Skip to content

Instantly share code, notes, and snippets.

@markekraus
Created November 25, 2020 21:03
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markekraus/a0135d0a5f52b609eda513df4db71cab to your computer and use it in GitHub Desktop.
Save markekraus/a0135d0a5f52b609eda513df4db71cab to your computer and use it in GitHub Desktop.
Example using HttpClient in PowerShell
# 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
@ChendrayanV
Copy link

Thanks @markekraus!

@Quazmoz
Copy link

Quazmoz commented Nov 1, 2023

This is helpful, thanks

@yowl
Copy link

yowl commented Jan 13, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment