Sample code for 30+ languages & platforms
Zig

OneNote - Get Page HTML Content

See more OneNote Examples

Download the HTML content of a OneNote page. Also iterates over the images and attachments on the OneNote page and downloads the content for each.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    // Implements the following CURL command:

    // curl -X GET https://graph.microsoft.com/v1.0/me/onenote/pages/{id}/content?includeIDs=true \
    //   -H 'authorization: Bearer ACCESS_TOKEN'

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    // Adds the "Authorization: Bearer ACCESS_TOKEN" header.
    http.setAuthToken("ACCESS_TOKEN");

    http.setUrlVar("page_id", "0-d2298668edd74dccac7f821fa378bf64!41-3A33FCEB9B74CC15!20350") catch {};

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb("https://graph.microsoft.com/v1.0/me/onenote/pages/{$page_id}/content?includeIDs=true", sb_response_body) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("HTML content:\n", .{});
    std.debug.print("{s}\n", .{try sb_response_body.getAsString(alloc)});

    const resp_status_code = http.getLastStatus();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try http.getLastHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Let's get the images and attached files.
    // Here's sample HTML content with 2 images and one PDF attachment:

    // <html lang="en-US">
    //     <head>
    //         <title>A page with rendered images and an attached file</title>
    //         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    //         <meta name="created" content="2020-10-23T12:00:00.0000000" />
    //     </head>
    //     <body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">
    //         <div id="div:{b130eb6c-638a-4f97-8f7e-b6d9e2e88bf9}{32}" data-id="_default" style="position:absolute;left:48px;top:120px;width:624px">
    //             <p id="p:{b130eb6c-638a-4f97-8f7e-b6d9e2e88bf9}{39}" style="margin-top:5.5pt;margin-bottom:5.5pt">Here&#39;s an image from an online source:</p>
    //             <img id="img:{ee18fe8d-b219-4baf-9b4d-4fc680579f0d}{1}" alt="an image on the page" width="500" height="500" src="https://graph.microsoft.com/v1.0/users('admin@chilkat.io')/onenote/resources/0-e71b45b763484921b4200e32c2439a47!1-3A33FCEB9B74CC15!20350/$value" data-src-type="image/jpeg" data-fullres-src="https://graph.microsoft.com/v1.0/users('admin@chilkat.io')/onenote/resources/0-e71b45b763484921b4200e32c2439a47!1-3A33FCEB9B74CC15!20350/$value" data-fullres-src-type="image/jpeg" />
    //             <p id="p:{b130eb6c-638a-4f97-8f7e-b6d9e2e88bf9}{43}" style="margin-top:5.5pt;margin-bottom:5.5pt">Here&#39;s an image uploaded as binary data:</p>
    //             <img id="img:{ee18fe8d-b219-4baf-9b4d-4fc680579f0d}{3}" alt="an image on the page" width="300" height="225" src="https://graph.microsoft.com/v1.0/users('admin@chilkat.io')/onenote/resources/0-a60516b962b842f2ae9bec75c16b31de!1-3A33FCEB9B74CC15!20350/$value" data-src-type="image/jpeg" data-fullres-src="https://graph.microsoft.com/v1.0/users('admin@chilkat.io')/onenote/resources/0-a60516b962b842f2ae9bec75c16b31de!1-3A33FCEB9B74CC15!20350/$value" data-fullres-src-type="image/jpeg" />
    //             <p id="p:{b130eb6c-638a-4f97-8f7e-b6d9e2e88bf9}{47}" style="margin-top:5.5pt;margin-bottom:5.5pt">Here&#39;s a file attachment:</p>
    //             <object data-attachment="FileName.pdf" type="application/pdf" data="https://graph.microsoft.com/v1.0/users('admin@chilkat.io')/onenote/resources/0-9e685efb33a24a6a8de1b9c707d28385!1-3A33FCEB9B74CC15!20350/$value" />
    //         </div>
    //     </body>
    // </html>

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.loadSb(sb_response_body, true) catch {};

    // Iterate over each element in the XML.  Each time we find an "img" or "object", download the data.
    // (The sbState is an object used to keep track of the current state of the traversal.)
    const sb_state = try chilkat.StringBuilder.init();
    defer sb_state.deinit();
    var url: [:0]const u8 = "";
    const bd = try chilkat.BinData.init();
    defer bd.deinit();
    while (xml.nextInTraversal2(sb_state)) {
        if (xml.tagEquals("img")) {
            std.debug.print("img id: {s}\n", .{try xml.getAttrValue(alloc, "id")});
            url = try xml.getAttrValue(alloc, "src");
            bd.clear() catch {};
            http.quickGetBd(url, bd) catch {
                std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
                return;
            };

            std.debug.print("Downloaded from {s}\n", .{url});
            std.debug.print("----\n", .{});
            // If desired, bd.WriteFile to save the contents of bd to a file..
        }

        if (xml.tagEquals("object")) {
            std.debug.print("data-attachment: {s}\n", .{try xml.getAttrValue(alloc, "data-attachment")});
            std.debug.print("type: {s}\n", .{try xml.getAttrValue(alloc, "type")});
            url = try xml.getAttrValue(alloc, "data");
            bd.clear() catch {};
            http.quickGetBd(url, bd) catch {
                std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
                return;
            };

            std.debug.print("Downloaded from {s}\n", .{url});
            std.debug.print("----\n", .{});
            // If desired, bd.WriteFile to save the contents of bd to a file..
        }
    }

    std.debug.print("Success.\n", .{});
}