I am trying to get an thumbnail image from the Microsoft Computer Vision Get Thumbnail API and save the response as a base64 encoded string that can be used in an html img tag like so:
<img src="data:image/gif;base64,...base64string..."></img>
If I start from a test.jpeg image I have on my local drive, I am able to generate a usable base64string:
[convert]::ToBase64String((get-content 'c:\test\test.jpeg' -encoding byte)) >> b64str.txt
When I try with data returned from the REST API I don't get get a usable base64string (I do get a string but it is not usable in the html img tag):
$acs_req_headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$acs_req_headers.Add("Ocp-Apim-Subscription-Key", "...subscriptionkey...")
$acs_req_body = '{"url":"https://...url.../test/test.jpg"}'
[convert]::ToBase64String(([System.Text.Encoding]::UTF8.GetBytes((Invoke-RestMethod 'https://...url...cognitiveservices.azure.com/vision/v3.2/generateThumbnail?width=100&height=100&smartCropping=true&model-version=latest' -Method 'POST' -Headers $acs_req_headers -Body $acs_req_body -ContentType 'application/json')))) >> b64str.txt
I've been working on this for some days, and the above is as close as I have gotten to a string that looks close. The issue seems to be the format returned from the API service and the subsequent conversion to bytes and then base64string but I'm lost as to where to go from here.
I tried to get an image from google images with
Invoke-RestRequestand I didn't manage to convert it to a valid base64 string.But, with
Invoke-WebRequestI managed to do so because the content is already in byte format:I saw from here that
RestRequestdoes some conversion under the hood? Maybe that's what causing the issues.Give
Invoke-WebRequesta shot. But don't use it asInvoke-RestRequestbecause it returns aWebResponseObjectobject which contains aContentproperty where your actual response will be.