Skip to content

Instantly share code, notes, and snippets.

@SingingBush
Created September 12, 2019 12:05
Show Gist options
  • Save SingingBush/d75dcbb855e7bf4d044abba21c5be6e8 to your computer and use it in GitHub Desktop.
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
$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