1

The OneNote API supports capturing images on to pages by including the image data in a POST. It requires a named <img> tag in the "Presentation" block of a multi-part request and binary image data in a part having the name from the <img> tag.

I cannot figure out how to embed the image data in the HTML body for the Invoke-RestMethod cmdlet such that the API renders the image on to the OneNote page. Who can help?

The closest I can get to an undistorted image is by following the "Content-type:image/jpeg" declaration by a blank line and by reading the image data UTF7-encoded from file.

$html = @"
--BlockBoundary
Content-Disposition:form-data; name="Presentation"
Content-type:text/html
<!DOCTYPE html>
  <html>
    <head><title>Page $Counter</title></head>
    <body><img src="name:TheImage"/></body>
  </html>
--BlockBoundary
Content-Disposition:form-data; name="TheImage"
Content-type:image/jpeg

$( Get-Content 'Image.jpg' -Raw -Encoding UTF7 )
--BlockBoundary--
"@

Invoke-RestMethod -Method Post `
    -Uri         'https://www.onenote.com/api/v1.0/pages' `
    -Headers     @{"Authorization" = "Bearer " + $AccessToken} `
    -ContentType 'multipart/form-data; boundary=BlockBoundary' `
    -Body        $html 

The code snippet assumes Powershell 3.0 or higher, a valid access token being stored in the variable $AccessToken and an accessible image file.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
wfr
  • 155
  • 9

1 Answers1

4

The main problem with the above snippet is that it was trying to save the image raw data within a string. Existing posts like How to send multipart/form-data with PowerShell Invoke-RestMethod talk about how to send multipart requests properly (The trick is to use the -InFile param in theInvoke-RestMethod cmdlet which reads the request body from a file).

Here's one way in which I was able to successfully generate a request:

# first build the request body prefix (everything before the image raw data)
$requestBodyPrefix = @"
--BlockBoundary
Content-Disposition:form-data; name="Presentation"
Content-type:text/html
<!DOCTYPE html>
  <html>
    <head><title>Page $Counter</title></head>
    <body><img src="name:TheImage"/></body>
  </html>
--BlockBoundary
Content-Disposition:form-data; name="TheImage"
Content-type:image/jpeg

"@
# save the prefix to a file 
Add-Content 'RequestBodySavedToFile' $requestBodyPrefix

#now read the image raw data and append to the file
$imageData = Get-Content 'Image.jpg' -Raw -Encoding Byte
Add-Content 'RequestBodySavedToFile' $imageData -Encoding Byte

# lastly, append the terminating boundary suffix
$requestBodySuffix = @"

--BlockBoundary--
"@
Add-Content 'RequestBodySavedToFile' $requestBodySuffix

#Invoke-RestMethod using the -InFile param
Invoke-RestMethod -Method Post `
    -Uri 'https://www.onenote.com/api/v1.0/pages' `
    -Headers @{"Authorization" = "Bearer " + $AccessToken} `
    -ContentType 'multipart/form-data; boundary=BlockBoundary' `
    -InFile 'RequestBodySavedToFile' 
Community
  • 1
  • 1
DipakBoyed
  • 434
  • 2
  • 5