Thursday, 15 August 2013

How to include image data in the HTML body using OneNote's REST API from Powershell? -



How to include image data in the HTML body using OneNote's REST API from Powershell? -

the onenote api supports capturing images on pages including image info in post. requires named <img> tag in "presentation" block of multi-part request , binary image info in part having name <img> tag.

i cannot figure out how embed image info in html body invoke-restmethod cmdlet such api renders image on onenote page. can help?

the closest can undistorted image next "content-type:image/jpeg" declaration blank line , reading image info utf7-encoded file.

class="lang-psh prettyprint-override">$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, valid access token beingness stored in variable $accesstoken , accessible image file.

the main problem above snippet trying save image raw info within string. existing posts how send multipart/form-data powershell invoke-restmethod talk how send multipart requests (the trick utilize -infile param in theinvoke-restmethod cmdlet reads request body file).

here's 1 way in able generate request:

# first build request body prefix (everything before 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 prefix file add-content 'requestbodysavedtofile' $requestbodyprefix #now read image raw info , append file $imagedata = get-content 'image.jpg' -raw -encoding byte add-content 'requestbodysavedtofile' $imagedata -encoding byte # lastly, append terminating boundary suffix $requestbodysuffix = @" --blockboundary-- "@ add-content 'requestbodysavedtofile' $requestbodysuffix #invoke-restmethod using -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'

html image powershell onenote

No comments:

Post a Comment