Sample code for 30+ languages & platforms
Zig

SMSAPI - Create a Contact

See more SMSAPI Examples

Create a Contact

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.smsapi.com/contacts -H "Authorization: Bearer token_api_oauth" \
    // -d "phone_number=48500000000&email=bok@smsapi.com&first_name=Name&last_name=Last_name&gender=gender&description=description&city=city&groups=default"

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

    const req = try chilkat.HttpRequest.init();
    defer req.deinit();
    req.setHttpVerb("POST");
    req.setPath("/contacts");
    req.setContentType("application/x-www-form-urlencoded");
    req.addParam("phone_number", "48500000000");
    req.addParam("email", "bok@smsapi.com");
    req.addParam("first_name", "Name");
    req.addParam("last_name", "Last_name");
    req.addParam("gender", "gender");
    req.addParam("description", "description");
    req.addParam("city", "city");
    req.addParam("groups", "default");

    req.addHeader("Authorization", "Bearer token_api_oauth");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpReq("https://api.smsapi.com/contacts", req, 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)

    // {
    //   "id": "5b802315a788494a04690d1d",
    //   "first_name": "string",
    //   "last_name": "string",
    //   "phone_number": "48500000000",
    //   "email": "bok@smsapi.com",
    //   "gender": "gender",
    //   "city": "city",
    //   "date_created": "2018-08-24T17:24:05+02:00",
    //   "date_updated": "2018-08-24T17:24:05+02:00",
    //   "description": "description",
    //   "groups": [
    //     {
    //       "id": "59a3ca1fa78849062837cd0c",
    //       "name": "default",
    //       "date_created": "2017-08-28T09:45:35+02:00",
    //       "date_updated": "2017-08-28T09:45:35+02:00",
    //       "description": "",
    //       "created_by": "username",
    //       "idx": null,
    //       "contact_expire_after": null,
    //       "contacts_count": null
    //     }
    //   ]
    // }

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

    const date_created = try chilkat.DtObj.init();
    defer date_created.deinit();
    const date_updated = try chilkat.DtObj.init();
    defer date_updated.deinit();
    var name: [:0]const u8 = "";
    var created_by: [:0]const u8 = "";
    var idx: [:0]const u8 = "";
    var contact_expire_after: [:0]const u8 = "";
    var contacts_count: [:0]const u8 = "";

    var id: [:0]const u8 = try j_resp.stringOf(alloc, "id");
    _ = try j_resp.stringOf(alloc, "first_name");
    _ = try j_resp.stringOf(alloc, "last_name");
    _ = try j_resp.stringOf(alloc, "phone_number");
    _ = try j_resp.stringOf(alloc, "email");
    _ = try j_resp.stringOf(alloc, "gender");
    _ = try j_resp.stringOf(alloc, "city");
    j_resp.dtOf("date_created", false, date_created) catch {};
    j_resp.dtOf("date_updated", false, date_updated) catch {};
    var description: [:0]const u8 = try j_resp.stringOf(alloc, "description");
    var i: i32 = 0;
    const count_i = j_resp.sizeOfArray("groups");
    while (i < count_i) {
        j_resp.setI(i);
        id = try j_resp.stringOf(alloc, "groups[i].id");
        name = try j_resp.stringOf(alloc, "groups[i].name");
        j_resp.dtOf("groups[i].date_created", false, date_created) catch {};
        j_resp.dtOf("groups[i].date_updated", false, date_updated) catch {};
        description = try j_resp.stringOf(alloc, "groups[i].description");
        created_by = try j_resp.stringOf(alloc, "groups[i].created_by");
        idx = try j_resp.stringOf(alloc, "groups[i].idx");
        contact_expire_after = try j_resp.stringOf(alloc, "groups[i].contact_expire_after");
        contacts_count = try j_resp.stringOf(alloc, "groups[i].contacts_count");
        i = i + 1;
    }
}