Sample code for 30+ languages & platforms
Zig

MercadoLibre - Create Test User

See more MercadoLibre Examples

Crea un usuario de test

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();

    // First get our previously obtained OAuth2 access token.
    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    try json_token.loadFile("qa_data/tokens/mercadolibre.json");

    // Implements the following CURL command:

    // curl -X POST -H "Content-Type: application/json" -d
    // '{
    //        "site_id":"MLA"
    // }'
    // https://api.mercadolibre.com/users/test_user?access_token=MY_ACCESS_TOKEN

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

    // {
    //   "site_id": "MLA"
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("site_id", "MLA") catch {};

    http.setRequestHeader("Content-Type", "application/json");

    http.setUrlVar("access_token", try json_token.stringOf(alloc, "access_token")) catch {};

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://api.mercadolibre.com/users/test_user?access_token={$access_token}", 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)});

    // A response status code of 201 indicates success.
    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": 577815702,
    //   "nickname": "TETE5320415",
    //   "password": "qatest4890",
    //   "site_status": "active",
    //   "email": "test_user_80450945@testuser.com"
    // }

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

    _ = j_resp.intOf("id");
    _ = try j_resp.stringOf(alloc, "nickname");
    _ = try j_resp.stringOf(alloc, "password");
    _ = try j_resp.stringOf(alloc, "site_status");
}