Created
September 12, 2019 12:05
-
-
Save SingingBush/d75dcbb855e7bf4d044abba21c5be6e8 to your computer and use it in GitHub Desktop.
Iterate images in a directory and make a POST request for each one to an API
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
$baseUrl = 'http://api.domain.com/some-service/' | |
Get-ChildItem -Path ".\*.jpg" | Sort-Object Name | % { | |
$response = try { | |
Invoke-RestMethod -uri "$baseUrl/image" -Method Post -Infile $_.Name -ContentType 'image/jpeg' -ErrorAction Stop | |
} catch [System.Net.WebException] { | |
Write-Error "Media Service returned $($_.Exception.Response.StatusCode.Value__) : $($_.Exception.Message)" | |
Exit 1; | |
} | |
write-host "Postimg image for " -nonewline; | |
write-host $_.BaseName -nonewline -foregroundcolor cyan; | |
write-host " {0}" -f $response.id; | |
New-Object PSObject -Property @{ | |
filename = $_.Name; | |
id = $response.id; | |
} | |
} | Export-Csv 'image-upload-result.csv' -Encoding UTF8 -Delimiter "," -Force -NoTypeInformation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment