Sample code for 30+ languages & platforms
Zig

OneNote - Create Notebook

See more OneNote Examples

Creates a new notebook in Microsoft OneNote

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 POST https://graph.microsoft.com/v1.0/me/onenote/notebooks \
    //   -H 'authorization: Bearer ACCESS_TOKEN'
    //   -H "Content-type: application/json" \
    //   -d '{
    //   "displayName": "Notebook name"
    // }'

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

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "displayName": "Testing Notebook"
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("displayName", "Testing Notebook") catch {};

    http.setRequestHeader("Content-type", "application/json");
    // Adds the "Authorization: Bearer ACCESS_TOKEN" header.
    http.setAuthToken("ACCESS_TOKEN");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://graph.microsoft.com/v1.0/me/onenote/notebooks", json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    resp.getBodySb(sb_response_body) catch {};
    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.loadSb(sb_response_body) catch {};
    j_resp.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    const resp_status_code = resp.getStatusCode();
    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 resp.getHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('admin%40chilkat.io')/onenote/notebooks/$entity",
    //   "id": "0-3A33FCEB9B74CC15!20344",
    //   "self": "https://graph.microsoft.com/v1.0/users/admin@chilkat.io/onenote/notebooks/0-3A33FCEB9B74CC15!20344",
    //   "createdDateTime": "2020-10-22T21:59:03.137Z",
    //   "displayName": "Testing Notebook",
    //   "lastModifiedDateTime": "2020-10-22T21:59:03.137Z",
    //   "isDefault": false,
    //   "userRole": "Owner",
    //   "isShared": false,
    //   "sectionsUrl": "https://graph.microsoft.com/v1.0/users/admin@chilkat.io/onenote/notebooks/0-3A33FCEB9B74CC15!20344/sections",
    //   "sectionGroupsUrl": "https://graph.microsoft.com/v1.0/users/admin@chilkat.io/onenote/notebooks/0-3A33FCEB9B74CC15!20344/sectionGroups",
    //   "createdBy": {
    //     "user": {
    //       "id": "4193973766868700757",
    //       "displayName": "Joe Smith"
    //     }
    //   },
    //   "lastModifiedBy": {
    //     "user": {
    //       "id": "4193973766868700757",
    //       "displayName": "Joe Smith"
    //     }
    //   },
    //   "links": {
    //     "oneNoteClientUrl": {
    //       "href": "onenote:https://d.docs.live.net/3a33fceb9b74cc15/Documents/Testing%20Notebook"
    //     },
    //     "oneNoteWebUrl": {
    //       "href": "https://onedrive.live.com/redir.aspx?cid=3a33fceb9b74cc15&page=edit&resid=3A33FCEB9B74CC15!20344"
    //     }
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    _ = try j_resp.stringOf(alloc, "\"@odata.context\"");
    _ = try j_resp.stringOf(alloc, "id");
    _ = try j_resp.stringOf(alloc, "self");
    _ = try j_resp.stringOf(alloc, "createdDateTime");
    _ = try j_resp.stringOf(alloc, "displayName");
    _ = try j_resp.stringOf(alloc, "lastModifiedDateTime");
    _ = j_resp.boolOf("isDefault");
    _ = try j_resp.stringOf(alloc, "userRole");
    _ = j_resp.boolOf("isShared");
    _ = try j_resp.stringOf(alloc, "sectionsUrl");
    _ = try j_resp.stringOf(alloc, "sectionGroupsUrl");
    _ = try j_resp.stringOf(alloc, "createdBy.user.id");
    _ = try j_resp.stringOf(alloc, "createdBy.user.displayName");
    _ = try j_resp.stringOf(alloc, "lastModifiedBy.user.id");
    _ = try j_resp.stringOf(alloc, "lastModifiedBy.user.displayName");
    _ = try j_resp.stringOf(alloc, "links.oneNoteClientUrl.href");
    _ = try j_resp.stringOf(alloc, "links.oneNoteWebUrl.href");
}