Sample code for 30+ languages & platforms
Zig

X.com Create Post

See more X Examples

Causes the User to create a Post under the authorized account.

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://api.x.com/2/tweets" \
    // -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
    // -H "Content-Type: application/json" \
    // -d '{"text":"Hello, X.com! This is my first post using the API."}'

    // 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.

    // {
    //   "text": "Hello, X.com! This is my first post using the API."
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("text", "Hello, X.com! This is my first post using the API.") catch {};

    // Adds the "Authorization: Bearer YOUR_ACCESS_TOKEN" header.
    // Get our access token.
    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/x.json") catch {
        std.debug.print("Failed to load x.json\n", .{});
        return;
    };

    // Adds the "Authorization: Bearer <token>" header.
    http.setAuthToken(try json_token.stringOf(alloc, "access_token"));

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://api.x.com/2/tweets", 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)

    // {
    //   "data": {
    //     "id": "1346889436626259968",
    //     "text": "Hello, X.com! This is my first post using the API."
    //   }
    // }

    // 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, "data.id");
    _ = try j_resp.stringOf(alloc, "data.text");
}