Sample code for 30+ languages & platforms
Zig

MercadoLibre - Cargar factura (Upload Invoice)

See more MercadoLibre Examples

Cargar factura en detalle de venta

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 https://api.mercadolibre.com/packs/$PACK_ID/fiscal_documents?access_token=$ACCESS_TOKEN
    //   -H 'content-type: multipart/form-data;'
    //   -F 'fiscal_document=@/home/user/.../Factura_adjunta.pdf'

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

    const sb_path = try chilkat.StringBuilder.init();
    defer sb_path.deinit();
    sb_path.append("/packs/$PACK_ID/fiscal_documents?access_token=$ACCESS_TOKEN") catch {};
    // Use an actual pack ID.
    var num_replaced: i32 = sb_path.replace("$PACK_ID", "2000000089077943");
    num_replaced = sb_path.replace("$ACCESS_TOKEN", try json_token.stringOf(alloc, "access_token"));

    const req = try chilkat.HttpRequest.init();
    defer req.deinit();
    req.setHttpVerb("POST");
    req.setPath(try sb_path.getAsString(alloc));
    req.setContentType("multipart/form-data");
    try req.addFileForUpload2("fiscal_document", "/home/user/.../Factura_adjunta.pdf", "application/pdf");

    req.addHeader("Expect", "100-continue");
    req.addHeader("content-type", "multipart/form-data;");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpSReq("api.mercadolibre.com", 443, true, 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)

    // {
    //   "ids": [
    //     "415460047_a96d8dea-38cd-4402-938e-80a1c134fc5d"
    //   ]
    // }

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

    var str_val: [:0]const u8 = "";

    var i: i32 = 0;
    const count_i = j_resp.sizeOfArray("ids");
    while (i < count_i) {
        j_resp.setI(i);
        str_val = try j_resp.stringOf(alloc, "ids[i]");
        i = i + 1;
    }
}